我已经在 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