的ActiveRecord :: HasManyThroughOrderError

Cj *_*aro 6 ruby-on-rails

我在3个模型之间有一个has_one关联,但它有一个错误,上面写着"ActionView :: Template :: Error(不能有一个has_many:通过关联'Policy#intermed',在通过关联之前通过'Policy#invoice'定义.)"

政策模式

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
Run Code Online (Sandbox Code Playgroud)

中介模型

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
Run Code Online (Sandbox Code Playgroud)

发票模型

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
Run Code Online (Sandbox Code Playgroud)

Mar*_*son 14

对于其他任何人 - 请参阅他们的主题https://github.com/rails/rails/issues/29123

为我解决的是切换has_ones的顺序

has_one :intermediary, through: :invoice, foreign_key: :intrmdry_intm_no
has_one :invoice
Run Code Online (Sandbox Code Playgroud)

  • 我把belongs_to代码放在我的has_many代码上面,这个代码为我解决了.欢呼喝彩! (2认同)