seeds.rb failing with SQLite3::ConstraintException on relationship
I am using seeds.rb to create two records, one of which belongs to the other:
new_account = Account.create(name: 'AccountName')
new_user = User.create(
email: 'foo@example.com',
password: 'pass',
password_confirmation: 'pass',
account: new_account)
This fails with a SQLite3::ConstraintException
, complaining that I'm not setting account_id
. It succeeds if I explicitly set the ID:
new_account = Account.create(name: 'AccountName')
new_user = User.create(
email: 'foo@example.com',
password: 'pass',
password_confirmation: 'pass',
account_id: new_account.id)
My understanding is that the former appr开发者_运维知识库oach should work - am I wrong about that, or am I just doing it wrong? I'm on Rails 3.1.0, with Devise 1.4.7.
it should not work the first way, as shown by your error. You must explicitly specify the id like you have done in the second example. :)
精彩评论