简体   繁体   English

检查has_many关系中的所有记录是否与Rails SQL查询中的条件匹配

[英]Checking that all records in a has_many relationship match a condition in rails SQL query

I am attempting to create an SQL query that filters based on a specific attribute of a record in a has_many relationship. 我正在尝试创建一个基于has_many关系中记录的特定属性进行过滤的SQL查询。

I have program.tests which have a has_many ivr_engagements, and I want to find tests that have no ivr_engagements with ivr_engagement.call_hook_number ==2. 我有具有has_many ivr_engagements的program.test,并且我想使用ivr_engagement.call_hook_number == 2查找没有ivr_engagements的测试。 Essentially the count of test.ivr_engagements with call_hook_number == 2 should be zero. 本质上,具有call_hook_number == 2的test.ivr_engagements的计数应该为零。

The object: 物体:

>> test.ivr_engagements
=> [#<IvrEngagement id: 281, user_id: 438431, testa_id: 508351, call_count: 1, call_hook_number: 1, qualified_response: true, ivr_application_id: 6741>, 
#<IvrEngagement id: 311, user_id: 438431, testa_id: 508351, call_count: 1, call_hook_number: 2, qualified_response: true, ivr_application_id: 6741>]

One of these ivr_engagements has a call_hook_number of 2. I would like to filter out any test that has an ivr_engagement matching that criteria. 这些ivr_engagements中的一个具有2的call_hook_number。我想过滤掉所有具有与该条件匹配的ivr_engagement的测试。

This query is currently returning true (I want it to be false, since there is one record test.ivr_engagements with call_hook_number as 2): 该查询当前返回true(我希望它为false,因为有一条记录test.ivr_engagements的call_hook_number为2):

>> program.tests.find(:all, :include => [:ivr_engagements], :conditions => "ivr_engagements.call_hook_number != 2").include? test
=> true
>> 

I've additionally tried: 我还尝试过:

>> program.tests.find(:all, :include => [:ivr_engagements], :conditions => "NOT EXISTS(SELECT 1 FROM ivr_engagements WHERE ivr_engagements.call_hook_number = 2)").include? test
=> false

Which I thought was working, but then it should have returned true when I removed the problem ivr_engagement: 我以为是可行的,但是当我删除问题ivr_engagement时,它应该返回true:

>> test.ivr_engagements
=> [#<IvrEngagement id: 281, user_id: 438431, testa_id: 508351, call_count: 1, call_hook_number: 1, qualified_response: true, ivr_application_id: 6741>, 
#<IvrEngagement id: 311, user_id: 438431, testa_id: 508351, call_count: 1, call_hook_number: 2, qualified_response: true, ivr_application_id: 6741>]
>> test.ivr_engagements.last.destroy
=> #<IvrEngagement id: 311, user_id: 438431, testa_id: 508351, call_count: 1, call_hook_number: 2, qualified_response: true, ivr_application_id: 6741>
>> program.reload.tests.find(:all, :include => [:ivr_engagements], :conditions => "NOT EXISTS(SELECT 1 FROM ivr_engagements WHERE ivr_engagements.call_hook_number = 2)").include? test
=> false

Also tried: 还尝试了:

>> program.tests.find(:all, :include => [:ivr_engagements], :conditions => "(SELECT count(*) FROM ivr_engagements WHERE ivr_engagements.call_hook_number = 2)=0")

But this also didn't work. 但这也不起作用。

Hoping to find the correct query. 希望找到正确的查询。 Thanks in advance! 提前致谢!

In that case you can work with a Join, like in your example, you already have the program, then we can go with this. 在这种情况下,您可以使用Join,例如在您的示例中,您已经有该程序,那么我们可以继续进行下去。

program.tests.joins(:ivr_engagements).where(:ivr_engagements => {call_hook_number: 2})

we find the tests that have ivr_engagements with call_hook_number with a value of 2. you can send conditions for the tests too in the same query. 我们发现具有ivr_engagements且其call_hook_number值为2的测试。您也可以在同一查询中发送测试条件。

as a side comment, if you later have another cases, the joins part, needs to be like the relationship, singular or plural, but the second, on the where, needs to be plural, because it's the name of the table. 作为附带说明,如果以后有其他情况,则联接部分需要像单数或复数关系,但第二个问题在何处需要复数,因为这是表的名称。 (in this case is the same, but if you are looking for a belongs to, it matters) (在这种情况下是相同的,但是如果您要寻找一个属于,则很重要)

Get test_ids of programs 获取程序的test_id

test_ids = program.tests.pluck(:id)

Now get IvrEngagement according to your condition. 现在根据您的情况获得IvrEngagement

IvrEngagement.where("testa_id IN (?) AND call_hook_number = ?", test_ids, 2)

Alternatively you can also try: => 或者,您也可以尝试:=>

Test.includes(:ivr_engagements).where("ivr_engagements.call_hook_number = ?", 2).distinct

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Active Record查询通过关系查找与Rails has_many中的所有条件匹配的记录 - Active Record query to find records that match all conditions in Rails has_many through relationship 多个has_many关系Rails SQL查询 - Multiple has_many relationship Rails SQL query SQL查询通过关系在has_many中查找具有某些记录的记录 - SQL Query to find records having certain records in a has_many through relationship Rails3 ActiveRecord-获取所有具有A和B的记录(通过has_many关系) - Rails3 ActiveRecord - Fetching all records that have A and B (through a has_many relationship) Ruby on Rails-具有条件的has_many关系需要联接 - Ruby on Rails - has_many relationship with condition that needs a join 带有Postgres的Rails 4 has_many关联:查询表A以获取实际上具有B,C和D的所有记录 - Rails 4 has_many associations w/Postgres: query table A for all records that actually have B, C, and D 一对多关系需要所有记录中的所有记录来匹配条件 - One to many relationship needs all of the many records to match a condition Rails 4查询记录,其中嵌套的has_many为空 - Rails 4 query records where nested has_many is empty 手写具有has_many关系的SQL查询 - Hand writing a SQL query with a has_many relationship Rails如何在不存在实例的情况下查询has_many实例 - Rails how to query has_many instance in condition of the instance not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM