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 使用带有LIKE和OR参数的作用域在两个不同的列中搜索_Ruby On Rails 3_Activerecord_Arel - Fatal编程技术网

Ruby on rails 3 使用带有LIKE和OR参数的作用域在两个不同的列中搜索

Ruby on rails 3 使用带有LIKE和OR参数的作用域在两个不同的列中搜索,ruby-on-rails-3,activerecord,arel,Ruby On Rails 3,Activerecord,Arel,做一个“喜欢”和/或通过争论的最佳方式是什么 我有一个自动完成字段,用户可以在其中键入邮政编码(数字)或郊区名称(字符串),并且很喜欢在两个不同的列中触发搜索 目前我正在做以下工作,但它不起作用: scope :search_by_postcode, lambda { |q| where("postcode LIKE ? OR postcode_name LIKE ?", "%#{q}%", "%#{q}%").limit(10) } 解决这个问题的方法是什么?值得考虑的是: 然后,您的范围将

做一个“喜欢”和/或通过争论的最佳方式是什么

我有一个自动完成字段,用户可以在其中键入邮政编码(数字)或郊区名称(字符串),并且很喜欢在两个不同的列中触发搜索

目前我正在做以下工作,但它不起作用:

scope :search_by_postcode, lambda { |q| where("postcode LIKE ? OR postcode_name LIKE ?", "%#{q}%", "%#{q}%").limit(10) }
解决这个问题的方法是什么?

值得考虑的是:

然后,您的范围将如下所示:

scope :first_ten_by_postcode_or_suburb_name, lambda { |q| where{postcode.matches(q) | suburb_name.matches("%#{q}%")}.limit(10) }

谢谢你的回答,我最终实现了一个类似的解决方案,但使用了Arel:q=“#{params[:term]}%”t=Location.Arel\u table locations=Location.where(t[:postcode].matches(q).或(t[:postcode\u name].matches(q))。limit(10)@Digger你有没有看过灵魂伴侣宝石的自动完成功能?如果您希望您的UI更具响应性,这可能是值得的。什么不起作用?怎么了?