belongs_to through associations

前端 未结 7 862
自闭症患者
自闭症患者 2020-11-30 17:19

Given the following associations, I need to reference the Question that a Choice is attached through from the Choice model. I have bee

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 17:57

    You can also delegate:

    class Company < ActiveRecord::Base
      has_many :employees
      has_many :dogs, :through => :employees
    end
    
    class Employee < ActiveRescord::Base
      belongs_to :company
      has_many :dogs
    end
    
    class Dog < ActiveRecord::Base
      belongs_to :employee
    
      delegate :company, :to => :employee, :allow_nil => true
    end
    

提交回复
热议问题