Ruby on rails 无效的源反射宏:有多个:通过

Ruby on rails 无效的源反射宏:有多个:通过,ruby-on-rails,ruby,activerecord,has-many-through,Ruby On Rails,Ruby,Activerecord,Has Many Through,我有这样愤怒的联想:融资>-事件>-子程序>-程序。我想通过所有程序访问最后的融资,因此代码是: class Fcp < Program has_many :fcp_subprograms, :foreign_key => 'parent_id' has_many :subprogram_last_actual_financings, :through => :fcp_subprograms, :sour

我有这样愤怒的联想:融资>-事件>-子程序>-程序。我想通过所有程序访问最后的融资,因此代码是:

class Fcp < Program
  has_many :fcp_subprograms,
           :foreign_key => 'parent_id'
  has_many :subprogram_last_actual_financings,
           :through => :fcp_subprograms,
           :source => :last_actual_financings

class FcpSubprogram < Program
  belongs_to :fcp,
             :class_name => '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 < ActiveRecord::Base
  belongs_to :fcp,
             :class_name => '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

但我的关联中有:source选项。我做错了什么?

这似乎是一种非常尴尬的做法。。。我宁愿在
程序中创建一个虚拟访问器,调用如下内容:

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

您得到的错误是关于源的反射是无效的关联,因为has\u many through的源必须属于、has\u one或has\u many without through选项。所以你不能用:last\u actual\u financings作为来源。

据我所知,你不能将double(或更多):through联系起来。您唯一能做的就是编写自己的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
这是给关系人->有很多->团队成员->有很多->团队角色->有一个团队


希望能有帮助。

但我需要最后一笔资金,用于所有子程序中的所有事件,然后进行汇总。融资模型的作用类似于融资如何随日期周期变化的历史。因此,真正重要的是,融资只能持续到最后一天。为了计算一个程序的融资,我应该将所有程序事件的最后融资和所有子程序的所有事件的最后融资相加。诸如此类的东西,你可以通过使用rails-2.3分支的插件来尝试嵌套的,尽管它是实验性的。我还没试过。或者你也可以使用finder\u 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