無効なソース反射マクロ: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
だから私は、私はこのエラーを取得する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のオプションを介さずにする必要があり通じているため、無効な団体です。だから、あなたが使用することはできません:。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