【发布时间】:2015-11-08 17:24:22
【问题描述】:
这是我的模型:
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
我想获取所有没有关联problem areas 的Complaints。
我认为解决方案可能包含左连接?像这样的东西(虽然这似乎不起作用)
complaints = Complaint.all.joins(:complaints_problem_area).where(problem_area_id: nil)
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 activerecord rails-activerecord