0
我发现了answer,但它非常过时(2009)。如何在Rails 4中自动排序has_many关系?
我想知道如何对has_many关系进行排序,最好在模型中进行排序。
事情是这样的:
class Article < ActiveRecord::Base
has_many :comments, :order => 'created_at DESC'
end
我发现了answer,但它非常过时(2009)。如何在Rails 4中自动排序has_many关系?
我想知道如何对has_many关系进行排序,最好在模型中进行排序。
事情是这样的:
class Article < ActiveRecord::Base
has_many :comments, :order => 'created_at DESC'
end
你试过:
class Article < ActiveRecord::Base
has_many :comments, -> { order "created_at DESC" }
end
当你迁移到Rails 4.0版本有很多语法的变化。 选中此项:
http://edgeguides.rubyonrails.org/association_basics.html#scopes-for-has-many