繁体   English   中英

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

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

我正在尝试创建一个基于has_many关系中记录的特定属性进行过滤的SQL查询。

我有具有has_many ivr_engagements的program.test,并且我想使用ivr_engagement.call_hook_number == 2查找没有ivr_engagements的测试。 本质上,具有call_hook_number == 2的test.ivr_engagements的计数应该为零。

物体:

>> 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>]

这些ivr_engagements中的一个具有2的call_hook_number。我想过滤掉所有具有与该条件匹配的ivr_engagement的测试。

该查询当前返回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
>> 

我还尝试过:

>> 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

我以为是可行的,但是当我删除问题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

还尝试了:

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

但这也不起作用。

希望找到正确的查询。 提前致谢!

在这种情况下,您可以使用Join,例如在您的示例中,您已经有该程序,那么我们可以继续进行下去。

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

我们发现具有ivr_engagements且其call_hook_number值为2的测试。您也可以在同一查询中发送测试条件。

作为附带说明,如果以后有其他情况,则联接部分需要像单数或复数关系,但第二个问题在何处需要复数,因为这是表的名称。 (在这种情况下是相同的,但是如果您要寻找一个属于,则很重要)

获取程序的test_id

test_ids = program.tests.pluck(:id)

现在根据您的情况获得IvrEngagement

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

或者,您也可以尝试:=>

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM