【问题标题】:Rails named_scope syntax errorRails named_scope 语法错误
【发布时间】:2009-11-04 16:53:52
【问题描述】:

我有一个运动队数据库,我在表格/排名中显示。相关代码涉及到两个模型,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/models/tableposition.rb:4: 语法错误,意外 '\n',期待 tASSOC

我在网上搜索了解决方案,并尝试将花括号转换为 do ... 以括号结尾,但无济于事。任何想法将不胜感激。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

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

    更正的代码:

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

    【讨论】:

    • 谢谢。在这里,通过投票和接受答案来显示。绿色复选标记对帮助有类似问题的其他人大有帮助。
    • 会的。还没有足够的“代表”来做这件事,但是当我这样做时我会回来的。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多