Frage

Ich habe so böse Assoziationen: Finanzierungen> - Veranstaltungen> - Unterprogramme> - Programme. Ich möchte Zugang zu last_financings von Programmen durch alle von ihnen erhalten, also Code ist:

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

Also, wenn ich zugreifen möchten, um subprogram_last_actual_financings in after_initialize Funktion bekomme ich diesen Fehler

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

aber ich habe: source Option in meinen Assoziationen. Was mache ich falsch?

War es hilfreich?

Lösung

Der Fehler, den Sie erhalten, ist über source_reflection eine ungültige Assoziation ist, weil Quelle für has_many durch, ohne durch Option belongs_to, has_one oder has_many sein muss. So können Sie nicht verwenden. Last_actual_financings als Quelle

Andere Tipps

Soweit ich erinnere Sie nicht zusammen mit Doppel machen kann (oder mehr): durch. Das einzige, was Sie tun können Sie Ihre eigenen SQL-Abfragen ist schreiben.

Hier ist mein Beispiel, wie es geht:

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

Dies ist für Beziehung Person -> has_many -> team_members -> has_many -> team_roles -> has_one -. Team

Hope es hilft.

Dies scheint eine wirklich unangenehme Art und Weise, dies zu tun ... Ich würde eher eine virtuelle Accessor in Program machen, die so etwas wie dies nennt:

self.subprograms.first.events.first.financings.first(:order => 'date DESC')
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top