源反射宏无效:has_many:through

Ant*_*ect 2 ruby activerecord ruby-on-rails has-many-through

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

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
Run Code Online (Sandbox Code Playgroud)

因此,当我想在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.
Run Code Online (Sandbox Code Playgroud)

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

Ser*_*bra 6

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

  • 你可以试试nested_has_many_through插件http://github.com/ianwhite/nested_has_many_through使用rails-2.3分支,虽然它是实验性的.我没试过.或者您可以对has_many使用finder_sql选项 (3认同)