我目前正在实施Ransack搜索功能。
我有一个模型广告系列,只要用户属于同一个供应商,它就会与用户直接创建的广告系列以及其他广告系列进行协作。
我可以将结果结合起来:
@search = current_user.campaigns + current_user.vendor.campaigns.where.not(:user_id => current_user.id)
问题在于Ransack不接受这种组合并吐出
no implicit conversion of Ransack::Search into Array
有人能指出我如何重构此代码的方向吗?
TIA
添加添加数据
查看我的控制台时,我可以看到* current_user.campaigns *:
Campaign Load (0.3ms)
SELECT DISTINCT "campaigns".* FROM "campaigns"
WHERE "campaigns"."user_id" = ? [["user_id", 2]]
运行* current_user.vendor.campaigns *给我:
Campaign Load (0.4ms)
SELECT DISTINCT "campaigns".* FROM "campaigns"
INNER JOIN "weeks" ON "campaigns"."id" = "weeks"."campaign_id"
INNER JOIN "products" ON "weeks"."product_id" = "products"."id"
INNER JOIN "locations" ON "products"."location_id" = "locations"."id"
WHERE "locations"."vendor_id" = ? [["vendor_id", 2]]
我可以通过以下方式获得current_user的第一个过滤器:
@search = Campaign.where("campaigns.user_id" => current_user.id).search(params[:q])
但我迷失了如何构建其余的连接表以包含两个数据元素
答案 0 :(得分:0)
<强>解决强>
@search = Campaign.includes(:weeks).where('(campaigns.user_id LIKE ?) OR (weeks.vendor_id LIKE ?)', current_user.id, current_user.vendor.id).search(params[:q])