這裏是我的模型:查找具有豐富的內部沒有相關記錄的所有記錄加入臺導軌
class Complaint < ActiveRecord::Base
has_many :complaints_problem_areas
has_many :problem_areas, through: :complaints_problem_areas
end
# rich join table for complaints and problem areas
class ComplaintsProblemArea < ActiveRecord::Base
belongs_to :complaint
belongs_to :problem_area
end
class ProblemArea < ActiveRecord::Base
has_many :complaints_problem_areas
has_many :complaints, through: :complaints_problem_areas
end
我想抓住所有的Complaints
沒有任何關聯problem areas
。
我認爲解決方案可能有左連接的東西?像這樣的東西(儘管這似乎並沒有工作)
complaints = Complaint.all.joins(:complaints_problem_area).where(problem_area_id: nil)
謝謝!我需要研究一些更多的「左連接」 – Neil