gpt4 book ai didi

ruby-on-rails - rails : One-to-many association fails due to foreign key validation

转载 作者:行者123 更新时间:2023-12-04 06:28:29 25 4
gpt4 key购买 nike

我已经在 rails 中设置了一对多关联,但是由于外键设置不正确,我的测试一直失败。我想知道是否有人有任何建议。

我有两个模型 - rota 和 user。我想要一个由用户“创建”的轮播。一个用户可以创建多个轮播。

测试失败

*在rota_spec中:*

it {should belong_to :creator}
Expected Rota to have a belongs_to association called creator (Rota does not have a creator_id foreign key.)

*在用户规范中:*
it {should have_many :created_rotas}
Expected User to have a has_many association called created_rotas (Rota does not have a creator_id foreign key.)

Rota.rb
  belongs_to :creator, :class_name => "User"

用户.rb
  has_many :created_rotas, :class_name => "Rota", :foreign_key => "creator_id"

迁移
class AddCreatorToRotas < ActiveRecord::Migration
def change
add_column :rotas, :creator_id, :string
end
end

最佳答案

你必须跑

rake db:test:prepare

关于ruby-on-rails - rails : One-to-many association fails due to foreign key validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11705772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com