Rails多次连接与查询的位置

时间:2014-07-29 11:54:17

标签: sql ruby-on-rails ruby devise

我在Rails应用程序中构建一些复杂查询时遇到问题。我有三种模式:

class Partner < ActiveRecord::Base
  belongs_to :rental_company
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :confirmable
end

class RentalCompany < ActiveRecord::Base
  has_many :owners, class_name: 'Partner'
  has_many :cars
end

class Car < ActiveRecord::Base
  belongs_to :rental_company
end

合作伙伴对象的方法是“已确认?”从设计。现在我想找到汽车租赁公司合作伙伴确认的所有汽车?是真的。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

这应该有效:

Car.joins(rental_company: :owners).where('partners.confirmed_at IS NOT NULL')

根据this,设计Confirmable模块将confirmed_at列设置为当前时间。因此,找到partnersconfirmed_at之外的NULL值的记录应该足够了。

相关问题