简体   繁体   English

有条件的热切嵌套嵌套关联

[英]Eager Load Nested Associations with Conditional

I think this is a lot simpler than the title probably lets on. 我认为这比标题可能要简单得多。 Here are my three models with the associations: 这是我与关联的三个模型:

Update : associations were incorrect previously. 更新 :关联以前不正确。 Here are the corrected associations: 这是更正的关联:

#app/models/call_service.category.rb
class CallServiceCategory < ActiveRecord::Base
  has_many :call_services
end

#app/models/call_service.rb
class CallService < ActiveRecord::Base
  belongs_to :call_service_category
  has_many :calls
end

#app/models/call.rb
class Call < ActiveRecord::Base
  belongs_to :call_service
end

So I have a group of call_ids for the calls I want: 所以我有一组我想要的电话的call_id:

@call_ids = [1,2,3,4]

Initial step which works : 第一步有效
What I want to do is grab only the calls with the ids specified in @call_ids . 我想做的是仅获取@call_ids指定的ID的calls Then, I want to eager load only the associated call_services for those grabbed calls . 然后,我只想为那些抓取的calls只加载相关的call_services The following does this perfectly: 下面可以完美地做到这一点:

@call_ids = [1,2,3,4]
@calls_by_service = CallService.includes(:calls).where("calls.id IN (?)", @call_ids).references(:calls)

This is great. 这很棒。 Now I can iterate through only those selected calls' call_services , and I can even list all of those selected calls per service like so: 现在,我仅可以遍历那些选定呼叫的call_services ,甚至可以按服务列出所有这些选定呼叫,如下所示:

<% @calls_by_service.each do |call_service| %>
  <p> <%= call_service.description %> </p>
  <% call_service.calls.each do |call| %>
    <%= call.name %><br>
  <% end %>
<% end %>

What is great about this too is that @calls_by_service does not contain ALL of the call_services , but instead only those call_service records associated with the calls specified in @call_ids . 这样做的@calls_by_service还在于, @calls_by_service不包含所有的call_services ,而是仅包含与@call_ids指定的calls相关联的那些call_service记录。 Exactly what I want at this level. 正是我想要的这个水平。

One Level Deeper which is where I am having trouble: 我遇到麻烦的是“更深层次”:
This is great but I need to go one level deeper: I want to display only the associated call_service_categories for the associated call_services of those selected calls specified by @call_ids . 很好,但是我需要更深入一点:我只想显示@call_ids指定的那些选定calls的关联call_service_categories的关联call_services

In other words: I want to grab only the calls with the ids specified in @call_ids . 换句话说:我只想抓取@call_ids指定的ID的calls Then: I want to eager load only the associated call_services for those grabbed calls . 然后:我只想为那些捕获的calls只加载关联的call_services Then: I want to eager load only the associated call_service_categories for those grabbed calls . 然后:我只想为那些捕获的calls仅加载关联的call_service_categories

A visual of the structure is like this: 结构的外观如下所示:

例

So I want to be able to iterate through those associated call_service_categories (ex: 'Emergency Relief', 'Employment'), and then iterate through the associated call_services of those calls specified in the @call_ids , and then display those calls per service. 因此,我希望能够遍历那些关联的call_service_categories (例如:“紧急救济”,“就业”),然后遍历@call_ids指定的那些calls的关联call_services ,然后按服务显示这些呼叫。

I figured out one level (by call_service), now I just need to figure out one level deeper (by call_service_category). 我找出了一个级别(通过call_service),现在我只需要找出一个更深的级别(通过call_service_category)。

In the rails guides, I attempted looking at the section on specifying conditions on eager loaded associations . 在rails指南中,我尝试查看有关指定渴望加载的关联条件的部分。 I was not having success, but I think the answer is in that section. 我没有成功,但我认为答案就在那部分。

Any help is much appreciated. 任何帮助深表感谢。 Thanks! 谢谢!

One of the belongs_to associations (in CallService or Call ) should be actually a has_one (one-to-one relationship – belongs_to on the one side and has_one on the other). (在CallServiceCallbelongs_to关联之一实际上应该是has_one (一对一关系–一侧是belongs_to ,另一侧是has_one )。 Apart from that, you can try the following code to produce a chained query with left join s and retrieve fields from all 3 tables: 除此之外,您可以尝试以下代码来生成带有left join的链接查询,并从所有3个表中检索字段:

CallServiceCategory.includes(call_services: :calls)
                   .where(calls: {id: @call_ids})
                   .references(:call_services, :calls)

I've noticed that you have a through association on your CallServiceCategory model, but as there would be no :call_services in includes , you can't reference fields from CallService model in references (you can, but they just won't appear in the actual sql query). 我注意到您在CallServiceCategory模型上具有through关联,但是由于includes没有:call_services ,因此您无法在referencesreferences CallService模型中的字段(可以,但是它们不会出现在实际的SQL查询)。

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

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