Ransack-分组使用或组合 [英] Ransack - Grouping to use or combinator

查看:87
本文介绍了Ransack-分组使用或组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Ransack gem的分组功能时,我遇到了一些问题.

I have some issues using the grouping functionalities of the Ransack gem.

我的搜索表中有很多条件,我想在它们之间进行OR运算,例如:

I have a lot of conditions in my search form and I want to perform a OR between to of them such as :

其中condition_x ="xxxx" AND condition_y ="yyyy" AND(website_present OR eshop_present)

WHERE condition_x = "xxxx" AND condition_y = "yyyy" AND (website_present OR eshop_present)

到目前为止,我所做的是在控制器中建立分组:

So so far what I did is building the grouping in my controller :

@q.build_grouping({:m => 'or', :website_present => "", :eshop_present => ""})

然后尝试在我的表单中显示它,如下所示:

And then tried to display it in my form as follow :

- @q.groupings.each do |g|
  - g.conditions.each do |c|
    - c.conditions_fields do |x|
      - x.value_fields do |v|
        = v.check_box :attribute, checked: :value

而且...什么也没有发生,没有错误,也没有显示.

And... nothing happen, no error and no display.

我也尝试了许多其他方式来显示它,但是没有任何效果,我找不到合适的示例来显示它.即使使用ransack演示,我也无法找到一种方法,而无需添加select_predicate和select_attribute字段.

I also tried many many other way to display it but nothing worked and I can't find a proper example to do it. Even with the ransack demo, I was unable to figure a way to do it without adding a select_predicate and select_attribute fields.

希望您能够帮助我

谢谢

推荐答案

问题已经很久了,我不知道它是否仍然有用,但是我今天也遇到了这个问题,如果有人需要的话会在这里留下答案

Question is quite old and I don't know if it is still relevant, but I had this issue too today and will leave answer here if someone will need it.

您似乎没有正确创建分组.这是对我有帮助的代码.

It looks like you were not creating groupings correctly. Here is the code that helped me.

在您的控制器中,您需要建立分组:

In you controller you need to build grouping:

     @q = SomeModel.search(params[:q])
     @q.build_build_grouping unless @q.groupings.any?

然后在您看来:

   = search_form_for @q do |f|
     = f.label :field_a_eq, 'Field A'
     = f.text_field :field_a_eq, 'aaa'
     = f.grouping_fields do |g|
       = g.hidden_field :m, value: 'or'
       = g.label :field_b_eq, 'Field B'
       = g.text_field :field_b_eq, 'bbb'
       = g.label :field_c_gteq, 'Field C'
       = g.text_field :field_c_gteq, 'ccc'

因此,您应该获得如下查询:

As result you should get query like this:

   WHERE field_a = "aaa" AND (field_b = "bbb" OR field_c >= "ccc")

这篇关于Ransack-分组使用或组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆