I have set up a one-to-many association in rails, but my test keeps on failing due to a foreign-key not set up properly. I was wondering if anyone had any advice.
I have two models - rota and user. I want a rota to be "created" by a user. A user can create more than one rota.
Test failing
*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
You must to run
rake db:test:prepare
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With