Aah,
it appears you misunderstand how the :order key works in a find,
it’s all a question of SQL.
Firstly,
you want to make your code efficient,
so you want to achieve your outcome in as few DB queries as possible.
For every has_many, or belongs_to, it needs to make a database query,
so instead, we’ll “eager” load these when we do our find.
all_with_attributes = Post.find(:all, :include => [:author, :comments]
but, actually, we don’t need this,
if you’re not using the comments, and only the time of the comment,
instead we’ll define a “last_comment” in your class
class Post < ActiveRecord::Base
belongs_to :author
has_many :comments
has_one :last_comment,
:class_name => “Comment”,
:order => “comments.created_at DESC”
end
This now let’s your write @post.last_comment.created_at
so, now our find should be;
Post.find(:all,
:include => [:author, :last_comment],
:order => “comments.created_at DESC”
)
And I think we’ll have what you need.
If that’s too confusing for you,
you can just do the following in ruby (rather than in the db)
Post.find(:all,
:include => [:author, :comments]
).sort_by{|p| p.comments.last.created_at}
- the include is optional, but seriously, you need to be doing this
- watch your development.log when you don’t
or
Post.find(:all,
:include => [:author, :comments],
:order => “comments.created_at DESC”
)
would probably work too.
Bob S. wrote:
I’m trying to order a table based on the last comment, and have tried
almost everything and still can’t get it working.
class Post < ActiveRecord::Base
belongs_to :author
has_many :comments
end
Consider the following loop using the class above:
for post in Post.find(:all)
puts "Post: " + post.title
puts "Written by: " + post.author.name
puts "Last comment on: " + post.comments.first.created_on
end