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 に答える