How do I set up a kind of “belongs_to :through” association without a direct belongs_to?

后端 未结 3 1029
感动是毒
感动是毒 2020-12-05 13:02

I know that \"belongs_to :through\" is not valid. It\'s just my attempt to express what I want to achieve. Just bear with me for a sec...

This is what I have:

<
3条回答
  •  时光取名叫无心
    2020-12-05 13:47

    Use delegate in the model class.

    class Team < ActiveRecord::Base
      belongs_to :division
      has_many :players
    
      delegate :league, to: :division
    end
    

    Reference: http://api.rubyonrails.org/classes/Module.html#method-i-delegate

提交回复
热议问题