英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论