4
我測試如下:RSpec的測試關係邏輯
帳戶
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
我真的不明白 - 父母是什麼?使用'create'方法時不應該創建和保存所有模型嗎?我已經嘗試過把'account.save'放在任何地方。
我認爲在某處存在驗證錯誤。所以#save或#create返回false。我自己面臨這個問題。在我的情況下,我是通過使用構建ie嘲笑返回類型即。不保存對象 – deepak 2012-09-25 12:55:12