【问题标题】:Invalid source reflection macro :has_many :through无效的源反射宏 :has_many :through
【发布时间】:2009-12-18 05:46:14
【问题描述】:

我有这样愤怒的联想:融资 >- 事件 >- 子程序 >- 程序。我想通过所有这些程序从程序中访问 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

所以当我想在 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 选项。我做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby activerecord has-many-through


    【解决方案1】:

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

    【讨论】:

    • 你可以尝试nested_has_many_through插件github.com/ianwhite/nested_has_many_through使用rails-2.3分支,虽然它是实验性的。我没试过。或者你可以为 has_many 使用 finder_sql 选项
    • 有趣的插件!我必须试一试
    • 非常感谢您的建议!
    【解决方案2】:

    据我所知,您无法与双(或更多)关联:通过。您唯一能做的就是编写自己的 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。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 但是我需要最后一次为程序的所有子程序中的所有事件提供资金才能将其汇总。融资模型的作用类似于融资在日期期间如何变化的历史。所以真正重要的只有最后期限的融资。要计算程序的融资,我应该将所有程序事件的最后融资和所有子程序的所有事件的最后融资相加。类似的东西
      猜你喜欢
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-04
      • 2016-01-20
      • 1970-01-01
      • 2014-09-01
      相关资源
      最近更新 更多