Rails: one-to-many relationship fails due to foreign key validation

I have established a one-to-many relationship on the rails, but my test continues to fail due to an incorrect foreign key setting. I was wondering if anyone has any advice.

I have two models - a company and a user. I want the company to be "created" by the user. User can create more than one company.

Testing error

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

* In 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" 

Migration

 class AddCreatorToRotas < ActiveRecord::Migration def change add_column :rotas, :creator_id, :string end end 
+4
source share
1 answer

You have to run

 rake db:test:prepare 
+7
source

All Articles