Newbie question on ordering e.g. @post.comments


#1

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


#2

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


#3

Yeh, or you could add the default order to the relationship, in the
model

like, has_many :comments, :order => “created_on desc”


#4

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 :slight_smile: