【问题标题】:Find 'homeless families' in many-to-many relation在多对多关系中找到“无家可归的家庭”
【发布时间】:2014-02-13 15:28:37
【问题描述】:

我有 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。

提前致谢!

【问题讨论】:

    标签: ruby-on-rails activerecord ruby-on-rails-3.2 many-to-many


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-09
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 2020-02-19
      相关资源
      最近更新 更多