2009-12-18 6 views
2

나는 파이낸싱> - 이벤트> - 서브 프로그램> - 프로그램과 같은 분노한 연관이 있습니다. 그래서잘못된 소스 리플렉션 매크로 : has_many : through

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 

나는이 오류

Invalid source reflection macro :has_many :through for has_many :subprogram_last_actual_financings, :through => :fcp_subprograms. Use :source to specify the source reflection. 

하지만이에게 얻을 after_initialize 기능에 subprogram_last_actual_financings에 액세스 할 때 코드가 그래서 나는 그들 모두를 통해 프로그램의 last_financings에 대한 액세스를 얻으려면 : 내 연결에서 소스 옵션. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 당신이 얻을 오류가 source_reflection에 관한

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

답변

6

가 잘못되었습니다이이 일을 정말 어색 방법처럼 보인다

+3

nested_has_many_through 플러그인 시도 http://github.com/ianwhite/nested_has_many_through 실험적이지만 rails-2.3 브랜치를 사용하십시오. 나는 그것을 시도하지 않았다. 또는 –

+0

재미있는 플러그인! 나는 그것을 시험해 봐야한다 – klew

+0

당신의 충고에 대해 대단히 고마운! – Antiarchitect

0

은 ... 차라리 이런 식으로 뭔가 호출하는 Program에 가상 접근을 할 것 has_many의 소스는 belongs_to, has_one 또는 has_many가 아니며 옵션을 통과하지 않아야합니다. 따라서 last_actual_financings을 소스로 사용할 수 없습니다.

+0

하지만 모든 프로그램의 모든 하위 프로그램에서 모든 이벤트에 마지막으로 자금을 조달해야 합산됩니다. Financing 모델은 날짜 기간을 통해 자금 조달이 어떻게 바뀌 었는지에 대한 이력과 같습니다. 그래서 정말로 수입자는 날짜 금융으로 만 마지막입니다. 프로그램의 자금 조달을 계산하려면 모든 프로그램의 이벤트에 대한 최종 자금을 합산하고 모든 서브 프로그램의 모든 이벤트에 대한 마지막 자금 조달을 합산해야합니다. 그와 비슷한 것 – Antiarchitect

3

내가 아는 한 당신은 이중 (또는 이상)과 연결할 수 없다는 것을 기억하십시오. 당신이 할 수있는 유일한 일은 당신 자신의 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 - 팀.

희망이 있습니다.

+0

조언 해 주셔서 감사합니다! – Antiarchitect

관련 문제