命名范围 - 绑定变量的错误数量

时间:2012-03-21 15:46:13

标签: ruby-on-rails

我有以下命名范围:

named_scope :report_search, lambda { |search|
  {
    :conditions => ["rep_name like ? or rep_id like ? or rep_symbol like ? or rep_issue like ?", search]
  }
}

当我运行它时,我收到错误消息:

wrong number of bind variables

如果有人能帮我弄清楚代码是错的,我将不胜感激。

非常感谢

2 个答案:

答案 0 :(得分:16)

如果您不想重复输入,可以使用命名绑定变量。

named_scope :report_search, lambda { |search|
  {
    :conditions => ["  rep_name   LIKE :search OR 
                       rep_id     LIKE :search OR 
                       rep_symbol LIKE :search OR 
                       rep_issue  LIKE :search", :search => search]
  }
}

答案 1 :(得分:11)

绑定语句中的每个问号都需要一个变量。如果它们完全相同,则需要多次重复:

named_scope :report_search, lambda { |search|
  {
    :conditions => ["rep_name like ? or rep_id like ? or rep_symbol like ? or rep_issue like ?", search, search, search, search]
  }
}