源reflection宏无效:has_many:through

我有这样愤怒的联想:融资> – 事件> – 子程序> – 程序。 我希望通过所有程序从程序访问last_financings,因此代码是:

class Fcp  'parent_id' has_many :subprogram_last_actual_financings, :through => :fcp_subprograms, :source => :last_actual_financings class FcpSubprogram  'Fcp', :foreign_key => 'parent_id' has_many :events, :foreign_key => 'fcp_id' has_many :last_actual_financings, :through => :events, :source => :last_actual_financings class Event  'Fcp', :foreign_key => 'fcp_id' belongs_to :fcp_subprogram, :class_name => 'FcpSubprogram', :foreign_key => 'fcp_id' has_many :last_actual_financings, :class_name => 'ActualFinancing', :order => 'date DESC', :limit => 1 

因此,当我想在after_initialize函数中访问subprogram_last_actual_financings时,我收到此错误

 Invalid source reflection macro :has_many :through for has_many :subprogram_last_actual_financings, :through => :fcp_subprograms. Use :source to specify the source reflection. 

但我有:我的协会中的源选项。 我究竟做错了什么?

您得到的错误是关于source_reflection是一个无效的关联,因为has_many的源必须是belongs_to,has_one或has_many而没有through选项。 所以你不能使用:last_actual_financings作为源。

据我所知,你不能与双重(或更多)联系:通过。 你唯一能做的就是编写自己的SQL查询。

这是我的例子如何做到这一点:

 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 

这是关系Person – > has_many – > team_members – > has_many – > team_roles – > has_one – team。

希望能帮助到你。

这似乎是一种非常尴尬的方式…我宁愿在Program中创建一个虚拟访问器来调用这样的东西:

 self.subprograms.first.events.first.financings.first(:order => 'date DESC')