Post has_many Comment
@post=Post.find(xxx)
@post.comments=post.comments.custom_order
@post.comments
@post.comments
I'd like to sort the comments, what should I do?
has_many
with scope.
has_many:comments, ->{order('created_at DESC')}
post=Post.find(1)
puts post.comments.to_sql
# SELECT "comments".* FROM "comments" WHERE "comments". "post_id" = 1ORDER BY created_at DESC
© 2025 OneMinuteCode. All rights reserved.