Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 按联接表属性从查询结果中删除一些检索到的记录_Sql_Ruby On Rails_Postgresql - Fatal编程技术网

Sql 按联接表属性从查询结果中删除一些检索到的记录

Sql 按联接表属性从查询结果中删除一些检索到的记录,sql,ruby-on-rails,postgresql,Sql,Ruby On Rails,Postgresql,我有一个类似的问题: SomeModel.includes(:some_join_table).where(name: name, some_join_table: {*** STUFF ***}) 在some_join_表中,有一个字段用于来自SomeModel的相关id和一个不允许的布尔列。如何让查询返回name==name的每个记录,而不包括id==id和disallowed==true的记录 不包括id==id和disallowed==true的记录 您可以将此条件转换为:x.id y

我有一个类似的问题:

SomeModel.includes(:some_join_table).where(name: name, some_join_table: {*** STUFF ***})
在some_join_表中,有一个字段用于来自SomeModel的相关id和一个不允许的布尔列。如何让查询返回name==name的每个记录,而不包括id==id和disallowed==true的记录

不包括id==id和disallowed==true的记录

您可以将此条件转换为:
x.id y.id
disallowed=false

不确定Ruby语法,但此查询将按照您的要求执行(只要定义了
disallowed
notnull
-我假设
id
是PK(不能为NULL):

选择m*
来自某个模型m
使用(名称)连接一些表j
其中m.id j.id或不允许j.id;

只需添加更多where子句即可

SomeModel.includes(:join_table).where(:name => name).
  where.not(:join_table => {:id => 10})
比如说

Post.includes(:comments).where(:subject => "Hello").
  where.not(:comments => {:id => 10})
Post.includes(:comments).where(:subject => "Hello").
  where.not(:comments => {:id => 10})