为这个令人困惑的标题道歉,我发现很难恰当地表达这个问题。
我有一个模型,称之为Thing
, has_many :related_things
, has_many :associated_dates
和has_many :assignments
(分配给事物的用户)。我想做一个单一的查询,从一个用户被分配到的Thing
中选择所有相关的日期,以及从相关的事情中选择所有相关的日期。
目前,我有这个用于获取直接分配的相关日期:
AssociatedDate.includes(:thing)
.includes(:thing => :assignments)
.where('assignments.user_id' => id)
然而,我试图也加载那些通过:related_things
相关的日期没有加载。我得到的最接近的是在查询中包括:related_things
:
AssociatedDate.includes(:thing)
.includes(:thing => [:assignments, :related_things])
.where('assignments.user_id' => id)
但是我不知道从那里去哪里,就其本身而言,这对查询的结果没有任何影响。我想要的选择是否可能,我该如何去做?
我认为你需要joins
。请看这里的文档。
AssociatedDate.joins(:thing).joins(:thing=>:related_thing)