Rails:由于外键验证,一对多关联失败

Kar*_*ran 4 migration ruby-on-rails associations one-to-many

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

我有两个型号 - 罗塔和用户.我希望用户"创建"一个rota.用户可以创建多个rota.

测试失败

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

*在user_spec中:*

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

Rota.rb

  belongs_to :creator, :class_name => "User"
Run Code Online (Sandbox Code Playgroud)

User.rb

  has_many :created_rotas, :class_name => "Rota", :foreign_key => "creator_id"
Run Code Online (Sandbox Code Playgroud)

移民

class AddCreatorToRotas < ActiveRecord::Migration
  def change
    add_column :rotas, :creator_id, :string
  end
end
Run Code Online (Sandbox Code Playgroud)

Dou*_*gui 9

你必须跑

rake db:test:prepare
Run Code Online (Sandbox Code Playgroud)