我的模特有Menu,MenuDrink和Drink。
协会:菜单has_many Menudrinks,has_many饮料通过MenuDrinks
MenuDrink表是一个中级表,有饮料的数量列。
数据示例
我尝试搜索菜单中有可乐: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"
为什么我的代码不起作用?
非常感谢您的支持!