Invalid source reflection macro :has_many :through

前端 未结 3 1278
野性不改
野性不改 2021-01-21 14:08

I have such angry associations: financings >- events >- subprograms >- programs. I want to get acces to last_financings from programs through all of them so code is:

<         


        
相关标签:
3条回答
  • 2021-01-21 14:44

    As far as I remember you can't make association with double (or more) :through. The only thing you can do is write your own sql queries.

    Here is my example how to do it:

    class Person
      ...
      has_many :teams, :finder_sql =>
        'SELECT DISTINCT teams.* FROM teams
            INNER JOIN team_roles ON teams.id = team_roles.team_id
            INNER JOIN team_members ON team_roles.id = team_members.role_id
            WHERE ((team_members.person_id = #{id}))'
    
      # other standard associations
      has_many :team_members
      has_many :team_roles,
        :through => :team_members
      # and I couldn't do:
      # has_many :teams, :through => :team_roles
    

    This is for relation Person -> has_many -> team_members -> has_many -> team_roles -> has_one - team.

    Hope it helps.

    0 讨论(0)
  • 2021-01-21 14:52

    This seems like a really awkward way of doing this... I'd rather make a virtual accessor in Program that calls something like this:

    self.subprograms.first.events.first.financings.first(:order => 'date DESC')
    
    0 讨论(0)
  • 2021-01-21 15:04

    The error you get is about source_reflection is an invalid association, because source for has_many through must be belongs_to, has_one or has_many without through option. So you can't use :last_actual_financings as a source.

    0 讨论(0)
提交回复
热议问题