how to save related models with non-null foreign key

Posted by Fortress on Stack Overflow See other posts from Stack Overflow or by Fortress
Published on 2011-02-06T07:21:45Z Indexed on 2011/02/06 7:25 UTC
Read the original article Hit count: 183

I have a model called user which has_one email. I put the foreign key (NOT NULL) inside users table. Now I'm trying to save it in the following way:

@email = Email.new(params[:email])
@email.user = User.new(params[:user])
@email.save

This raises a db exception, because the foreign key constraint is not met (NULL is inserted into email_id). How can I elegantly solve this or is my data modeling wrong?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord