无效源反射的宏:的has_many:通过
-
20-09-2019 - |
题
我有这样的愤怒协会:融资> - 事件> - 子程序> - 程序。我想通过他们都获得查看在程序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
所以,当我想访问subprogram_last_actual_financings在after_initialize功能我得到这个错误
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通过选项。所以你不能使用: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
这是关系人 - >的has_many - > team_members - >的has_many - > team_roles - > HAS_ONE - 队
希望它能帮助。
这似乎是这样做的真正尴尬的方式......我宁愿做在Program
虚拟访问调用是这样的:
self.subprograms.first.events.first.financings.first(:order => 'date DESC')
不隶属于 StackOverflow