0

我有2個模型和一個鏈接他們在多對多的關係。 像這樣:找到'無家可歸的家庭'在多對多的關係

class Family < ActiveRecord::Base 
     has_many :family_in_house 
    end 

    class House < ActiveRecord::Base 
     has_many :family_in_house 
    end 

    class FamilyInHouse < ActiveRecord::Base 
     belongs_to :family 
     belongs_to :house 
    end 

,我需要有不與任何房子相關的家庭單獨範圍。

我剛剛接觸RoR並且自己找不到解決方案。如果有必要,我使用Rails 3.2.9。

在此先感謝!

回答

1
class Family < ActiveRecord::Base 
    has_many :family_in_houses 

    scope :without_house, includes(:family_in_houses).where(:family_in_houses => {:house_id=>nil}) 
end 
+0

工作得很好,非常感謝! – Sergey