Forum: Ruby on Rails Newbie question on ordering e.g. @post.comments

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Piers Y. (Guest)
on 2006-06-08 21:01
Hi
Quick question (I think):
I've got the table joins/active record stuff working I think, but is
there a way I can order the query results?

E.g.
@post = Post.find(:all)
@comments = @post.comments
gives me all the comments on a particular post, but dated such that the
first comment is first on the page.
Could someone let me know how I can reverse that order so that it gives
me the last comment first?

Thanks - and sorry if it's a stupid question
Piers
Stephen B. (Guest)
on 2006-06-08 21:05
> Could someone let me know how I can reverse that order so that it gives
> me the last comment first?
You can just use the find() method:
@post.comments.find(:all, :order => 'created_on DESC')

Steve
David Andersen (Guest)
on 2006-06-08 21:10
(Received via mailing list)
Yeh, or you could add the default order to the relationship, in the
model

like, has_many  :comments, :order => "created_on desc"
Piers Y. (Guest)
on 2006-06-08 21:19
Stephen B. wrote:
>> Could someone let me know how I can reverse that order so that it gives
>> me the last comment first?
> You can just use the find() method:
> @post.comments.find(:all, :order => 'created_on DESC')
>
> Steve

Thanks Steve :)
This topic is locked and can not be replied to.