RSpec Validation Logic

I am testing the following:

Account

class Account < ActiveRecord::Base has_many :ownerships has_many :brands, :through => :ownerships end 

Ownership Model

 class Ownership < ActiveRecord::Base belongs_to :brand belongs_to :account end 

Test

 it "should be able to apply for brand ownership" do account = Account.create(valid_account_attributes) account.ownerships.create(:brand => Brand.create(:name => 'Superuser')) account.ownerships.first.state == 'pending' end 

And I keep getting this error

 You cannot call create unless the parent is saved 

I really don't understand - which parent? Shouldn't all models be created and saved when using the create method? I tried to hang "account.save".

+4
source share
2 answers

Are you sure account actually saved? Have you tried using create! to find out if there are any exceptions?

+1
source

I had the same error. I thought I deleted all the rows of my tables, but still had one with the user, the same user that I tried to insert using this command. I solved the problem by erasing the line.

0
source

All Articles