activerecord基于外表过滤掉

时间:2017-01-19 14:43:40

标签: ruby-on-rails activerecord

我正在尝试建立一个空缺有一个或多个时间表的协会。可能很多匹配

现在我需要获得在开始前2周的空缺,并且与州批准的内容没有匹配

Vacancy.includes(:schedules)
.where(schedules: {start_date: Date.today..Date.today+14})
.order("schedules.start_date ASC")
.includes(:matchings).where.not(matchings: {state: "approved"})

第一部分工作正常,我正在填补空缺并成功订购,但之后过滤掉与已批准匹配的部分无效。

2 个答案:

答案 0 :(得分:1)

你可以尝试一下吗?

Vacancy.joins(:schedules,:matchings).where(schedules: {start_date: Date.today..Date.today+14}).order("schedules.start_date ASC").where.not(matchings: {state: "approved"})

答案 1 :(得分:1)

你应该可以做这样的事情

Vacancy.includes(:schedules, :matchings)
.where(schedules: {start_date: Date.today..Date.today+14})
.order("schedules.start_date ASC")
.where.not(matchings: {state: "approved"})

它只是您遇到问题的操作顺序

我用自己的项目来写这个关系

Client.includes(:documents, :equipments).where(documents: {doc_type: 'equipment'} ).where(equipment: {shipper_id: 24}).count # 4

Client.includes(:documents, :equipments).where(documents: {doc_type: 'equipment'} ).where.not(equipment: {shipper_id: 24}).count # 314