2

私はそのような怒っている協会を持っています:資金調達>-イベント>-サブプログラム>-プログラム。プログラムから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オプションがあります。私は何が間違っているのですか?

4

3 に答える 3

3

私が覚えている限りでは、double (または more) :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

これは関係 Person -> has_many -> team_members -> has_many -> team_roles -> has_one - team 用です。

それが役に立てば幸い。

于 2009-12-21T09:42:46.720 に答える
0

これは、これを行うには非常に厄介な方法のように思えます...私はむしろ、次のProgramような呼び出しで仮想アクセサーを作成したいと思います:

self.subprograms.first.events.first.financings.first(:order => 'date DESC')
于 2009-12-18T16:16:07.433 に答える