包括多级外国表

时间:2017-01-19 17:14:32

标签: ruby-on-rails activerecord

空缺有匹配,匹配有房间,房间有消息

我需要获得符合匹配属性的特定条件的空缺,然后根据他们从员工那里得到的消息再次过滤它们。

Vacancy.created_this_week
.includes(:matchings, :rooms, :messages)
.where(matchings: {state: ["applied", "accepted", "denied"]})
.where(messages: {from_employee: false}.count

虽然我得到了以下内容:

Can't join 'Vacancy' to association named 'rooms'; perhaps you misspelled it?

我知道这种关联是基于匹配的,但我怎么能让它适合一个查询,因为我也需要过滤掉一些空缺?

修改

根据下面的答案我试过

.includes(matchings: { rooms: :messages })

哪个给了我

Can't join 'Matching' to association named 'room'; perhaps you misspelled it?

完整性检查:

>> Matching.first.room.messages
=> #<ActiveRecord::Associations::CollectionProxy []>

1 个答案:

答案 0 :(得分:2)

尝试使用哈希格式嵌套包含:

.includes(matchings: { rooms: :messages })