How do I automatically sort a has_many relationship in Rails?

前端 未结 5 1197
情书的邮戳
情书的邮戳 2020-12-12 13:02

This seems like a really simple question but I haven\'t seen it answered anywhere.

In rails if you have:

class Article < ActiveRecord::Base 
  has         


        
5条回答
  •  暖寄归人
    2020-12-12 13:36

    You can use ActiveRecord's find method to get your objects and sort them too.

      @article.comments.find(:all, :order => "created_at DESC")
    

    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

提交回复
热议问题