cat
cat

Reputation: 749

How can I fetch the records with order by nested column?

User

Community

Post

I'd like to show all the records of Posts at example.com/posts
But it has to be sorted by Post owner User's 'last_signed_in' column which is in Users table.

I tried this but this returns error undefined method `users' How can I solve?

@orders = @community.posts.user.order("last_active_at ASC")

Upvotes: 1

Views: 62

Answers (1)

Robin
Robin

Reputation: 915

You can not call a user object on a collection of posts.

But you can include the users in your collection:

@community.posts.includes(:user).order("users.last_signed_in")

Further informations: http://guides.rubyonrails.org/active_record_querying.html#eager-loading-multiple-associations

Upvotes: 3

Related Questions