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

后端 未结 3 1027
感动是毒
感动是毒 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:35

    You can define a helper method in your player model:

    def division
      team.division
    end
    
    def league
      team.division.league
    end
    

    Of course, this only relates to readability of your code and does not affect the form of the database queries involved. If your statement generates multiple SQL queries but you want only one, check out the .include option here: Rails Guides - Active Record Query Interface

提交回复
热议问题