Rails Order by Association Field

Rails order by association field

Try this:

@users = User.includes(:user_extension).order("user_extensions.company desc")

I think you need at order: user_extensions, not user_extension

Rails order by a field in parent belongs_to association

You'll need to use includes. Try:

@messages = current_user.messages.includes(:number).order('numbers.digits ASC')

How to Sort By Association Attribute Value in Rails

User.includes(:company).order('company.name') . I believe ASC is default, but you can add it if necessary. You may also have to specify the Company table name in the model, or change the table name to 'companies', as that is the default pluralization convention over configuration will assume I believe.

Rails ActiveRecord how to order by a custom named association

That is because there is no following_users table in your SQL query.

You will need to manually join it like so:

Following.
joins("
INNER JOIN users AS following_users ON
following_users.id = followings.following_user_id
").
where(user_id: 47). # use "followings.user_id" if necessary
includes(user: :followers).
order("following_users.username ASC")

To fetch Following rows that don't have a following_user_id, simply use an OUTER JOIN.

Alternatively, you can do this in Ruby rather than SQL, if you can afford the speed and memory cost:

Following.
where(user_id: 47). # use "followings.user_id" if necessary
includes(:following_user, {user: :followers}).
sort_by{ |f| f.following_user.try(:username).to_s }

Just FYI: That try is in case of a missing following_user and the to_s is to ensure that strings are compared for sorting. Otherwise, nil when compared with a String will crash.

Rails sort entries based on attributes of related model

You can order on the associated table by doing a nested join, like so:

@posts = Post.joins(:user => :friendships).order("friendships.friended_at")

Rails order by in associated model

There are multiple ways to do this:

If you want all calls to that association to be ordered that way, you can specify the ordering when you create the association, as follows:

class Log < ActiveRecord::Base
has_many :items, :order => "some_col DESC"
end

You could also do this with a named_scope, which would allow that ordering to be easily specified any time Item is accessed:

class Item < ActiveRecord::Base
named_scope :ordered, :order => "some_col DESC"
end

class Log < ActiveRecord::Base
has_many :items
end

log.items # uses the default ordering
log.items.ordered # uses the "some_col DESC" ordering

If you always want the items to be ordered in the same way by default, you can use the (new in Rails 2.3) default_scope method, as follows:

class Item < ActiveRecord::Base
default_scope :order => "some_col DESC"
end

Rails sort by association count and associated field on same model

So there are 2 problems here:

  1. You are using .joins on a belongs_to association that may not exist.
  2. You are joining the same user table twice (for 2 different associations), causing rails to generate a table alias so that the SQL will be valid. But you aren't grouping on this alias.

Solution:

You'll need a LEFT JOIN which unfortunately isn't as pretty as a standard join. You'll also need to modify your .group clause:

Department.
select("departments.*, COUNT(users.id) AS members").
joins(:members, "LEFT JOIN users directors ON departments.director_id = directors.id").
group("departments.id, directors.last_name").
order("members, directors.last_name")

How to sort by created_at column of association in rails?

You need to specify which table you want to use in the order by clause.

@posts = @user.favorited.order('posts.created_at DESC')

ought to do it.

One nice trick is to use the rails console when inspecting associations. Specifically, it helps to use the 'to_sql' method on Active Record queries you are performing.

For instance:

% bundle exec rails console

> u = User.last
> u.favorited.order('created_at DESC').to_sql


Related Topics



Leave a reply



Submit