So, imagine the following:
class User < ActiveRecord::Base
class Email < ActiveRecord::Base
class Address < ActiveRecord::Base
Now I am wondering what the correct Ruby-way to create a user object is
in the controller when through a form, a new user, a couple of email
addresses and a street address is posted.
The obvious @user = User.new(params[:user]) was my first attempt. The
problem here is that @user.emails.build and @user.address.build calls
are fine, until we try to save the @user object. It then gives an error
that it can not save the emails and address, because it doesn't know
what user_id to write in its foreign keys.
The only way I could devise was an intricate scheme where I would first
try to save the emails and address, and then the user object. Whenever
something would fail along the way it would rollback any previous
changes and destroy the correct object. This results in a very complex
non-ruby if/then stack which I really hate.
How am I supposed to save an object spanning multiple models at once and