Invalid source reflection macro :has_many :through

天涯浪子 提交于 2019-12-02 00:44:10

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.

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.

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')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!