4

我正在测试以下内容:

帐户

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

所有权加入模式

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

测试

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

我不断收到这个错误

You cannot call create unless the parent is saved

我真的不明白 - 什么父母?使用“创建”方法时,不应该创建和保存所有模型吗?我试过把'account.save'放在任何地方。

4

2 回答 2

1

你确定那account真的被保存了吗?您是否尝试使用create!查看是否引发任何异常?

于 2010-01-10T05:43:30.600 回答
0

我有同样的错误。我以为我已经删除了表的所有行,但仍然有一个用户,即我试图使用命令插入的同一用户。我通过删除线解决了这个问题。

于 2011-07-07T23:37:51.440 回答