0

我的帖子模型中有这个范围

scope :tynewydd_posts, :include => :department, :conditions => {"departments.name" => "Ty Newydd"}

我想按照创建的顺序对返回的结果进行排序,所以最新的帖子优先

我试过

scope :tynewydd_posts, :include => :department, :conditions => {"departments.name" => "Ty Newydd"}.order("posts.created_at DESC")

但我得到未定义的方法顺序:hash,所以我想我不能在这里使用这个方法?

4

2 回答 2

2

试试这个方法:

scope :tynewydd_posts, 
      :include => :department, 
      :conditions => {"departments.name" => "Ty Newydd"}, 
      :order => "posts.created_at DESC"
于 2013-02-16T09:46:34.230 回答
1

order是传递给范围的可能选项,如下所示:

scope :tynewydd_posts, :include => :department, :conditions => {"departments.name" => "Ty Newydd"}, :order => "posts.created_at DESC"

或者更好的是,正确的 Rails 3 语法,例如:

scope :tynewrdd_posts, includes(:department).where('departments.name' => 'Ty Newydd').order('posts.created_at DESC')

于 2013-02-16T10:02:10.630 回答