Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 范围描述的顺序结果_Ruby On Rails 3_Scope - Fatal编程技术网

Ruby on rails 3 范围描述的顺序结果

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" =

我的posts模型中有这个范围

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语法在速度方面有什么好处吗?