Ruby on rails 3 范围描述的顺序结果
我的posts模型中有这个范围Ruby on rails 3 范围描述的顺序结果,ruby-on-rails-3,scope,Ruby On Rails 3,Scope,我的posts模型中有这个范围 scope :tynewydd_posts, :include => :department, :conditions => {"departments.name" => "Ty Newydd"} 我想按创建的顺序对返回的结果进行排序,所以请先发布最新结果 我试过了 scope :tynewydd_posts, :include => :department, :conditions => {"departments.name" =
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,所以我想我不能在这里使用这个方法?试试这个方法:
scope :tynewydd_posts,
:include => :department,
:conditions => {"departments.name" => "Ty Newydd"},
:order => "posts.created_at DESC"
试着这样做:
scope :tynewydd_posts,
:include => :department,
:conditions => {"departments.name" => "Ty Newydd"},
:order => "posts.created_at DESC"
order
是传递给范围的一个可能选项,如下所示:
范围:tynewydd_posts,:include=>:department,:conditions=>{“departments.name”=>“tynewydd”},:order=>“posts.created_at DESC”
或者更好的是,正确的Rails 3语法,如:
scope:tynewrdd\u posts,包括(:department).where('departments.name'=>'tynewydd').order('posts.created\u at DESC')
order
是传递到范围的一个可能选项,如下所示:
范围:tynewydd_posts,:include=>:department,:conditions=>{“departments.name”=>“tynewydd”},:order=>“posts.created_at DESC”
或者更好的是,正确的Rails 3语法,如:
scope:tynewrdd_posts,包括(:department).where('departments.name'=>'tynewydd').order('posts.created_at DESC')
谢谢,使用rails 3语法在速度方面有什么好处吗?谢谢,使用rails 3语法在速度方面有什么好处吗?