- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个模型 - Publisher 和 Policy。政策属于发布者,发布者有很多政策。在政策模型中,有一个名为“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 列表中删除策略,并输出按发布者分组的剩余策略,而实际上我想查找具有此条件的发布者,而不是创建条件作为它似乎在做。我觉得这里有一个明显的错误,但我一直在努力寻找它。有什么想法吗?
最佳答案
您正在寻找:
publishers who [do] not have any policies associated with a specific ad_partner
所以就这么说吧。首先找到有此类政策的发布商:
Policy.where('ad_partner ilike ?', partner).select(:publisher_id)
然后找到不在该列表中的发布者:
Publisher.where.not(
id: Policy.select(:publisher_id).where('ad_partner ilike ?', partner)
)
在 Policy
查询中包含 select(:publisher_id)
将告诉 ActiveRecord 使用子查询,因此结果将是这样的 SQL:
select *
from publisher
where id not in (
select publisher_id
from policies
where ad_partner ilike 'indexexchange.com'
)
这将为您提供没有重复的发布商。然后您可以添加所需的任何顺序和限制。
您的原始查询无法按您希望的方式工作,因为 JOIN 将生成包含所有内容的行:
publisher1_columns, policy_with_partner
publisher1_columns, policy_without_partner
publisher1_columns, policy_without_partner
publisher2_columns, policy_without_partner
...
然后您的 WHERE 将仅排除 JOIN 中的第一行,留下您不想要的两个“publisher1”行。
说 ilike 'indexexchange.com'
只是一种不区分大小写的复杂方式。如果你想要模式匹配,那么你会说:
where('ad_partner ilike ?', "%#{partner}%")
相反。
关于sql - Rails 事件记录查询 : Join on Child Condition to include only matching results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49469162/
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicates: what is the difference between #include and #include “fi
我想使用 #include 指令,其文件名作为外部定义的宏传递。 例如 #include #FILE".h" 其中 FILE 将被定义为字符串 MyFile(不带引号),结果为 #include "M
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我想在当前目录及其子目录下的每个 .m 文件中查找所有出现 ncread 的情况。我使用以下命令: grep -R --include="\.m" ncread . 但是该命令没有返回任何内容。 gr
有时我会遇到这样的情况,我发现我需要为大型第三方文件制作一个#include,这样我才能使用一个函数或一个小类,这让我感到内疚,因为我知道这已经消失了增加我的编译时间,因为当我只想要一个功能时它会编译
这个问题在这里已经有了答案: 关闭13年前. Possible Duplicate: what is the difference between #include and #include “fi
我正在尝试通过应用程序加载器提交应用程序。我收到这个错误。但我已经检查了build设置,所有三种架构都包含在有效架构设置中。 最佳答案 断开任何设备,只保留“iOS 设备”中的选项并将其存档。 关于i
Please check this demo plunker更好地理解我的问题。 在我的主页上有一个表格。每个表行后面都有一个最初隐藏的空行。单击第一行时,我使用指令在其下方的空行中注入(inject
我正在使用 mkdocs 创建 html 网页和片段扩展以将我的主文档分成小块。我有一个难以理解的错误: 在我制作的文件file1.md中: --8<-- includes/some_rep/frag
include的推荐方式是什么?您项目的所有文件? 我见过很多使用类似结构的例子: include 的有序列表单个顶级文件(定义 Module 的文件,或应用程序中的“主”文件)中的语句。 这似乎也是
我想知道如何使用 fx:include与 JavaFX Scene Builder 结合使用,因此: 想象我有一个 BorderPane (文件 borderpane.fxml)。在中间部分我想放一个
我看到 Fortran 有“调用”和“包含”语句。两者有什么区别? .i 文件类型有什么意义吗? 即: include 'somefile.i' call 'somesubroutine.f' 谢谢!
这很挑剔,可能没有任何实际用途。我只是好奇... 在 C++20 工作草案 (n4861) 中, header 名称定义为: (5.8) header-name: " q-char-
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: What is the difference between #include and #include “fil
我有一个非常庞大且臃肿的类,我想将它拆分成单独的文件,但它应该对用户完全透明并且与使用该类的现有项目兼容。 特别是,我有自己的 ImageMatrix 类,它定义了大量的一元函数、大量带有标量的二元函
我是 grep 的新手,在重构 C 和 C++ 文件的过程中,我遇到了替换系统的问题,包括 #include <>与本地包括 #include "" . 有没有一种方法可以将 grep 与任何替代工具
我正在制作一个 Spring MVC web 项目,我必须有一个常量 header 。 我的基本要求是“我们希望在所有屏幕上都有一个标题,以显示谁登录了 ProjectA。” 我从这里“What is
在 SWIG 中,“%include”指令与标准 C“#include”有什么区别? 例如,在所有教程中,为什么它们通常看起来像这样: %module my_module %{ #include "M
假设我们有这个头文件: MyClass.hpp #pragma once #include class MyClass { public: MyClass(double); /* .
我已经在一个项目上工作了一段时间,该项目实现了一个使用 C 库的自定义框架。该框架是用 Swift 编写的,我创建了一个模块来向 Swift 公开 C 头文件。该框架是在不同的项目中启动的,然后将该框
我是一名优秀的程序员,十分优秀!