유효하지 않은 소스 반사 매크로 : has_many : 통과
-
20-09-2019 - |
문제
그런 화난 협회가 있습니다 : 금융>- 이벤트>- 하위 프로그램>- 프로그램. 나는 모든 프로그램을 통해 프로그램에서 acces를 얻고 싶습니다. 코드는 다음과 같습니다.
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 in after_initialize function에 액세스하려면이 오류가 발생합니다.
Invalid source reflection macro :has_many :through for has_many :subprogram_last_actual_financings, :through => :fcp_subprograms. Use :source to specify the source reflection.
그러나 나는 다음과 같습니다 : 내 협회의 소스 옵션. 내가 뭘 잘못하고 있죠?
해결책
has_many를 통한 소스는 옵션이 없어야한다는 소스가 소속 _to, has_one 또는 has_many가되어야하기 때문에 소스 _reflection에 관한 오류는 잘못된 연관성입니다. 따라서 사용할 수 없습니다 : 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