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

我有两个模型-旋转和用户。我希望由用户“创建”轮播。一个用户可以创建多个旋转。

测试失败的

*在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.)

*在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.)

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

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

10-07 21:49