Ruby on rails 搜查

Ruby on rails 搜查,ruby-on-rails,ransack,Ruby On Rails,Ransack,这是一个基本的问题,但我在报告中找不到明确的内容。我有以下代码: field = "secre" Position.search( {:description_cont => field, :code_cont => field}).result(:distinct => true).to_sql => "SELECT DISTINCT `positions`.* FROM `positions` WHERE ((`positions`.`description` L

这是一个基本的问题,但我在报告中找不到明确的内容。我有以下代码:

field = "secre"
Position.search( {:description_cont => field, :code_cont => field}).result(:distinct => true).to_sql
 => "SELECT DISTINCT `positions`.* FROM `positions`  WHERE ((`positions`.`description` LIKE '%secre%' AND `positions`.`code` LIKE 0))"
但我的问题应该是:

 => "SELECT DISTINCT `positions`.* FROM `positions`  WHERE ((`positions`.`description` LIKE '%secre%' OR `positions`.`code` LIKE 0))"

任何帮助都将不胜感激。提前感谢

在对代码进行了一段时间的挖掘之后,我按照如下方式进行了操作:

field = "secre"
q= {
  "m"=>"or", 
  "c"=>{
    "0"=>{
      "a"=>{
        "0"=>{
          "name"=>"description"
        }
      }, 
      "p"=>"cont", 
      "v"=>{
        "0"=>{
          "value"=>field
        }
      }
    }, 
    "1"=>{
      "a"=>{
        "0"=>{
          "name"=>"code"
        }
      }, 
      "p"=>"cont", 
      "v"=>{
        "0"=>{
          "value"=>field
        }
      }
    }
  }
}
Position.search(q).result(:distinct => true).to_sql
 => "SELECT DISTINCT `positions`.* FROM `positions`  WHERE ((`positions`.`description` LIKE '%secre%' OR `positions`.`code` LIKE 0))"

是的,这很恶心,当然这不应该是最好的方式,但它暂时救了我。还有其他想法吗?

这可能不是你问题的确切答案,但因为我在谷歌上搜索了正确的方法,或者搜索了一遍我自己,没有找到好的答案,但我自己解决了问题,所以我想我会分享解决方案

在我正在开发的一个应用程序中,有一个搜索页面,它将model
Customer
的各个字段作为参数(链接到DB table
customers
),然后列出一个与搜索结果匹配的客户表。客户有三个不同的电话号码字段,搜索页面以前对每种类型的号码都有不同的搜索字段。我想把它们合并成一个字段,方便地在所有数字中搜索

数据库有以下三个字段定义(在这些代码段中,我只更改了一些标识符名称):

以前,搜索页面(文件
app/views/customers/index.html.erb
)包含以下内容:

<%= search_form_for @search do |f| %> <!-- this is a Ransack-provided form -->
  <div class="field">
...
    <%= f.label :customer_phone_a_spaces_match_anything, "Phone number A is or contains:" %>
    <%= f.text_field :customer_phone_a_spaces_match_anything %>

    <%= f.label :customer_phone_b_spaces_match_anything, "Phone number B is or contains:" %>
    <%= f.text_field :customer_phone_b_spaces_match_anything %>

    <%= f.label :customer_phone_c_spaces_match_anything, "Phone number C is or contains:" %>
    <%= f.text_field :customer_phone_c_spaces_match_anything %>
...
  </div>

  <div class="actions">
    <%= f.submit "Search", class: "btn btn-large btn-primary" %>
  </div>

<% end %> <!-- search_form_for -->
这意味着除了Ransack的默认值
eq
cont
等之外,我还可以在搜索中使用自定义谓词
空格匹配任何内容。谓词按它所说的做。)

无论如何,从你做的同一个例子中获得灵感,我在model
app/models/customer.rb中添加了以下ransacker:

ransacker :all_phones do |parent|
  Arel::Nodes::InfixOperation.new('||',
    Arel::Nodes::InfixOperation.new('||',
      Arel::Nodes::InfixOperation.new('||', parent.table[:customer_phone_a]||"", ' '),
                                    parent.table[:customer_phone_b]||"", ' '),
                                  parent.table[:customer_phone_c]||"")
end
最后,我将搜索页面中的三个电话号码搜索字段替换为:

<%= f.label :customer_phone_a_or_customer_phone_b_or_customer_phone_c_cont, "Phone number is or contains:" %>
<%= f.text_field :customer_phone_a_or_customer_phone_b_or_customer_phone_c_cont %>

用户在此搜索字段中输入的数字现在将与客户的三个数字中的任何一个匹配。(请注意在ransacker中防止空数字
| |“”

这已在Ruby v1.9.3和Rails v3.2.8上进行了测试。输入参数将不匹配一个数字的结尾和下一个数字的开头,即使在正确的位置输入了空格,也不匹配,即使在搜索字段代码中,我用
\u spaces\u match\u anything
替换
\u cont
,请尝试以下操作:

Position.search( {:description_or_code_cont => field}).result(:distinct => true).to_sql

把这个放在你的搜索表单下。这是假设您正在使用f:

<%= f.combinator_select %>


它将生成一个包含两个选项的select。全部或任何。任何人都将使用OR子句。所有这些都将使用AND子句。

现在我们可以使用或查询ransack:

Position.ransack(description: 'secret').result.or(Position.ransack(code: 0).result)

您是否尝试过使用方括号[]而不是哈希{}语法?@MurifoX该方法不接受数组+1这是原始问题的答案。另外,对于has_x关系的属性,语法不是很明显,所以我把它放在这里是为了子孙后代。假设一个
老师有一个教学大纲
包含属性
foo
bar
老师。搜索(:教学大纲\u foo\u或\u教学大纲\u bar\u eq=>“val”)
Teemu,你的答案救了我几个小时后试图让类似的东西工作!!!!谢谢我很高兴在过去的7年里,对于ransack,情况有所改善:)
<%= f.combinator_select %>
Position.ransack(description: 'secret').result.or(Position.ransack(code: 0).result)