ActiveRecord::HasManyThroughOrderError

Viewed 3287

I have a has_one association between 3 models, but it have an error that says "ActionView::Template::Error (Cannot have a has_many :through association 'Policy#intermediary' which goes through 'Policy#invoice' before the through association is defined.)"

policy model

class Policy < ApplicationRecord

    self.table_name = "gipi_polbasic"
    self.primary_key = "policy_id"

    has_one :invoice
    has_one :intermediary, through: :invoice, foreign_key: :intrmdry_intm_no

intermediary model

class Intermediary < ApplicationRecord
    self.table_name = "giis_intermediary"
    self.primary_key = "intm_no"

    has_one :invoice, foreign_key: :intrmdry_intm_no
    belongs_to :policy, foreign_key: :policy_id

invoice model

class Invoice < ApplicationRecord
    self.table_name = "gipi_comm_invoice"
    self.primary_key = "intrmdry_intm_no"

    belongs_to :policy, foreign_key: :policy_id
    belongs_to :intermediary, foreign_key: :intrmdry_intm_no
4 Answers
Related