【发布时间】:2018-11-24 13:48:40
【问题描述】:
问题是我找不到为什么在创建新记录时无法插入引用列 id。
我有 3 个表 shop_plan、shop 和 app
以下是表格架构:
create_table "shop_plans", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "shops", force: :cascade do |t|
t.string "url"
t.bigint "plan_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["plan_id"], name: "index_shops_on_plan_id"
end
create_table "apps", force: :cascade do |t|
t.bigint "shop_id"
t.binint "amount"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["app_id"], name: "index_apps_on_shop_id"
end
add_foreign_key "shops", "shop_plans", column: "plan_id"
add_foreign_key "apps", "shops"
下面是模型
class ShopPlan < ApplicationRecord
has_many :shop
end
class Shop < ApplicationRecord
belongs_to :shop_plan, class_name: 'ShopPlan', foreign_key: :plan_id
has_many :app
end
class App < ApplicationRecord
belongs_to :shop, class_name: 'Shop', foreign_key: :shop_id
end
seed.db 中会为 shop_plan 表添加 1 条默认记录
ShopPlan.create(name: 'Basic')
ShopPlan 和 Shop 由Shop 中的plan_id 列链接
Shop和App由App中的shop_id列链接
我在用户访问索引时预先插入了一些值:
#basic_plan
@basicPlan = ShopPlan.where(name: "Basic").first
# if new shop registered, add to database
unless Shop.where(url: @shop_session.url).any?
shop = Shop.new
shop.url = @shop_session.url
shop.plan_id = @basicPlan.id
shop.save
end
但是,当我运行第二次插入时,此插入效果很好:
@shop= Shop.where(url: @shop_session.url).first
unless App.where(shop_id: @shop.id).any?
app = App.new
app.shop_id = @shop.id,
app.amount = 10
app.save
end
发生错误,因为app.shop_id 不会添加到我的@shop.id 中,它会返回错误:{"shop":["must exist"]}
我什至尝试硬编码app.shop_id =1,但它没有帮助,当我将optional: true 添加到 app.db 模型时,它会插入null
如果有人可以帮助指出我收到此错误的原因,请不胜感激
编辑:@arieljuod 要清楚
1)由于在Shop 和Shop_Plan 之间,我必须指定确切的列类,我使用手动plan_id 而不是使用默认的shopplans_id 列。
2) 我在 App 中更新了 1 列,除非只是在调试时进行检查。
【问题讨论】:
-
当你复数你的has_many关联时会发生什么,所以
has_many :shops和has_many :apps? -
@David 它仍然返回相同的结果,但只是提到我对
Shop的插入效果很好,问题只发生在App插入。还是谢谢你 -
为了使其正常工作,需要将 ShopPlan 中的关联更改为
has_many :shops, foreign_key: :plan_id。否则,rails 将无法从此端正确加入关联。您还可以将 App 中的关联更改为仅belongs_to :shop和belongs_to :shop_plan, foreign_key: :plan_id。class_name和foreign_key选项仅在无法从关联名称中推断出来时才需要。
标签: ruby-on-rails shopify