2017-07-30 94 views
0

我正努力使用has_one关联。 http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.htmlRails has_one left_outer_join关联

class Employee < ActiveRecord::Base 
    has_one :office 
end 
class Office < ActiveRecord::Base 
    belongs_to :employee # foreign key - employee_id 
end 

我想回谁没有办公的员工列表。我会期望使用以下内容,但这不起作用。

Employee.left_outer_joins(:office).where("office.id = null") 
+0

你得到任何错误? – Pavan

+0

道歉。它只是返回一个空的AR关系 – Dercni

+0

你是否有符合条件的记录? – Pavan

回答

0

您需要查询调整到低于

Employee.left_outer_joins(:office).where("office.id IS null") 

这也适用

Employee.left_outer_joins(:office).where(office: {id: nil}) 
+0

我在原来的问题中犯了一个错误,请将您的答案从left_outer_join更新到left_outer_joins – Dercni

+1

@Dercni完成!更新 – Pavan