Ransack:使用带有snake_case表的嵌套

时间:2017-10-16 19:36:46

标签: ruby-on-rails ruby rubygems

我的模特有Menu,MenuDrink和Drink。

协会:菜单has_many Menudrinks,has_many饮料通过MenuDrinks

MenuDrink表是一个中级表,有饮料的数量列。

数据示例

  • 菜单1可乐:2 /饮食可乐:3 /水:5
  • Menu2可乐:1 /饮食可乐:3 /水:2
  • 菜单3可乐:2 /饮食可乐:3 /水:3

我尝试搜索菜单中有可乐:2(菜单1和2适用于示例。)

-----Menu.rb------
has_many :menu_drinks
has_many :drinks, through: :menu_drinks

-----MenuDrink.rb-----
belongs_to :menu
belongs_to :drink

-----Drink.rb-------
has_many :menu_drinks
has_many :menus, through: :menu_drinks

q = {
    "g" => {
        "0" => {
            "g" => {
                "c" => {
                    "0" => {
                        "a" => { "0" => { "name" => "menu_drinks_quantity" } },
                        "p" => "eq",
                        "v" => { "0" => { "value" => 2 } }
                    }
                }
            },
        },
        "1" => {
            "g" => {
                "c" => {
                    "0" => {
                        "a" => { "0" => { "name" => "drinks_drink_name" } },
                        "p" => "eq",
                        "v" => { "0" => { "value" => "coke" } }
                    }
                }
            },
        }
    },
    "m" => "and"
}


Menu.ransack(q).result
=> Select "menu"...... 
    WHERE ("drinks"."drink_name" = 'coke' AND "menu_drinks_menus"."quantity" = 2)

回报与我的预期不同。

我认为" menu_drinks"。" number",not" menu_drinks_menus "。" number"

为什么我的代码不起作用?

非常感谢您的支持!

0 个答案:

没有答案