I’ve searched the Agile… book and tried google etc. but my main problem
is not being able to think of keywords.
I’m trying to save something with user info, and then further info if
they are staff in a one-to-one relationship. I can do this all fine and
@user = User.new(params[:user])
@staff_member = StaffMember.new(params[:staff_member])
@user.staff_member = @staff_member
if @user.save and @staff_member.save
flash[:notice] = ‘StaffMember was successfully created.’
redirect_to :action => ‘list’
render :action => ‘new’
However, if either of the models returns an error, the form gets
redisplayed with the errors (all well and good), but the successful
object is still saved.
EG. If I don’t provide a staff_member e-mail address (required), then
the app still goes ahead and saves the user info anyway.
How do I stop it saving the other model if one fails.
I thought about an if statement inside an if statement, but thought
there must be a more elegant solution in Rails.
Help much appreciated.