Rails多重STI关系has_many

huangapple go评论55阅读模式
英文:

rails multiple STI relation has_many

问题

我有这些模型

class OfferWrapper < ApplicationRecord

end

class PurchaseOfferWrapper < OfferWrapper
  has_many :purchase_offers, dependent: :destroy
end

class Offer < ApplicationRecord
end

class PurchaseOffer < Offer
  belongs_to :purchase_offer_wrapper, foreign_key: :offer_wrapper_id, inverse_of: :purchase_offer
end

我的迁移

def change
  create_table :offer_wrappers do |t|
    t.string :ref
    t.timestamp
  end
  add_reference :offers, :offer_wrapper
end

但是当我尝试从PurchaseOfferWrapper访问purchase_offer时,我遇到了一个foreign_key错误,类似于这样

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column offers.purchase_offer_wrapper_id does not exist
LINE 1: ...s" WHERE "offers"."type" IN ('PurchaseOffer') AND "offers"."...
                                                             ^
: SELECT COUNT(*) FROM "offers" WHERE "offers"."type" IN ('PurchaseOffer') AND "offers"."purchase_offer_wrapper_id" = $1
[4] pry(main)> PurchaseOfferWrapper.last.purchase_offers.to_sql
  PurchaseOfferWrapper Load (0.6ms)  SELECT  "offer_wrappers".* FROM "offer_wrappers" WHERE "offer_wrappers"."type" IN ('PurchaseOfferWrapper') ORDER BY "offer_wrappers"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> "SELECT \"offers\".* FROM \"offers\" WHERE \"offers\".\"type\" IN ('PurchaseOffer') AND \"offers\".\"purchase_offer_wrapper_id\" = 4806"

我在belongs_to中使用foreign_key选项强制使用 offer_wrapper_id,但它不起作用。它仍然尝试使用 purchase_offer_wrapper_id

我的错误在哪里?

提前感谢。

英文:

I have theses models

class OfferWrapper < ApplicationRecord

end

class PurchaseOfferWrapper < OfferWrapper
  has_many :purchase_offers, dependent: :destroy
end

class Offer < ApplicationRecord
end

class PurchaseOffer < Offer
  belongs_to :purchase_offer_wrapper, foreign_key: :offer_wrapper_id, inverse_of: :purchase_offer
end

My migration

def change
  create_table :offer_wrappers do |t|
    t.string :ref
    t.timestamp
  end
  add_reference :offers, :offer_wrapper
end

but when I try to access to a purchase_offer from a purchase_offer_wrapper i have a foreign_ky error like this

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column offers.purchase_offer_wrapper_id does not exist
LINE 1: ...s" WHERE "offers"."type" IN ('PurchaseOffer') AND "offers"."...
                                                             ^
: SELECT COUNT(*) FROM "offers" WHERE "offers"."type" IN ('PurchaseOffer') AND "offers"."purchase_offer_wrapper_id" = $1

[4] pry(main)> PurchaseOfferWrapper.last.purchase_offers.to_sql
  PurchaseOfferWrapper Load (0.6ms)  SELECT  "offer_wrappers".* FROM "offer_wrappers" WHERE "offer_wrappers"."type" IN ('PurchaseOfferWrapper') ORDER BY "offer_wrappers"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> "SELECT \"offers\".* FROM \"offers\" WHERE \"offers\".\"type\" IN ('PurchaseOffer') AND \"offers\".\"purchase_offer_wrapper_id\" = 4806"

I use foreign_key option in my belongs_to to force offer_wrapper_id , but it dosen't work. it still try to use purchase_offer_wrapper_id.

Where is my mistake ?

Thanks in avdance

答案1

得分: 1

好的,最终我找到了问题。

我还必须在我的has_many关联中添加foreign_key选项。

class PurchaseOfferWrapper < OfferWrapper
  has_many :purchase_offers, foreign_key: :offer_wrapper_id, dependent: :destroy
end
英文:

Okay, finaly I found the problem.

I have to add foreign_key option in my has_many association too.

class PurchaseOfferWrapper < OfferWrapper
  has_many :purchase_offers, foreign_key: :offer_wrapper_id, dependent: :destroy
end

huangapple
  • 本文由 发表于 2023年4月19日 19:27:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定