Rails named_scope语法错误

时间:2009-11-04 16:53:52

标签: ruby-on-rails

我有一个运动队数据库,我在桌子/排名中显示。相关代码涉及两个模型,Tableposition和Draw,它们以has_one关系关联。以下静态命名范围声明非常有效:

class Tableposition < ActiveRecord::Base
  belongs_to :draw
  named_scope :grouptable, :include => :draw, :conditions => ['draws.group = ?', "B"]
end

然而,当我试图让它变得动态时:

class Tableposition < ActiveRecord::Base
  belongs_to :draw
  named_scope :grouptable, :include => :draw, 
                lambda { |group| { :conditions => ['draws.group = ?', group] } }
end

我收到以下错误:

SyntaxError:/... / app / model / tableposition.rb:4:语法错误,意外'\ n',期待tASSOC

我已经在网上搜索解决方案,并尝试将花括号转换为do ...以括号括起来无济于事。任何想法都会非常感激。

1 个答案:

答案 0 :(得分:4)

named_scope想要这样的参数:(name, options)你给它(name, include option, condition option)其中include和condition选项都是哈希值。相反,你需要给它一个合并的哈希值。

更正后的代码:

named_scope :grouptable, lambda { |group|
  {  :include => :draw, :conditions => ['draws.group = ?', group] } 
}