1
votes

User

  • has_many :communities
  • has_many :posts

Community

  • belongs_to :user
  • has_many :posts

Post

  • belongs_to :user
  • belongs_to :community

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")
1

1 Answers

3
votes

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