【发布时间】:2018-03-24 19:46:43
【问题描述】:
我有两个模型 - Publisher 和 Policy。策略属于发布者,发布者有许多策略。在政策模型中,有一个名为“ad_partner”的列,我希望找到在广告合作伙伴中没有包含特定值的政策的发布商。最终目标是列出没有与特定 ad_partner 关联的任何政策的发布商。
这是我当前的查询:
test = Publisher.joins(:policies).select('publishers.name,
publishers.id, publishers.domain').where.not("policies.ad_partner ILIKE
?", 'indexexchange.com').group("publishers.id,
publishers.name").limit(10)
输出如下所示:
+----+------------------+---------------------+
| id | name | domain |
+----+------------------+---------------------+
| 1 | Business Insider | businessinsider.com |
| 2 | Wired | wired.com |
| 3 | CNET | cnet.com |
| 4 | Weather Channel | weather.com |
| 5 | Time Inc | time.com |
| 6 | IGN | ign.com |
| 7 | CBS | cbs.com |
| 8 | NBC | nbc.com |
| 9 | TMZ | tmz.com |
| 10 | HGTV | hgtv.com |
+----+------------------+---------------------+
这是不正确的,因为我知道大多数(如果不是全部)这些发布商都有包含 indexexchange.com 作为 ad_partner 中的值的政策。我怀疑正在发生的事情是查询说“好的,我们正在从 JOIN 列表中删除策略,并输出按发布者分组的剩余策略,而实际上我想查找具有此条件的发布者,而不是创建条件为它似乎在做。我感觉这里有一个明显的错误,但我一直在努力寻找它。有什么想法吗?
【问题讨论】:
-
你有模特吗?请张贴。也请您的 schema.rb
标签: sql ruby-on-rails postgresql activerecord rails-activerecord