On Tue, 2006-07-04 at 00:50 +0200, James Darling wrote:> Hi,
> 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
> good.
>
> def create
> @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''
> else
> render :action => ''new''
> end
> end
>
> 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.
----
sounds like you want to validate before you save...
if @user.validate && @staff_member.validate
@user.save
@staff_member.save
blah...
end
I think ''&&'' is a better choice if you are doing ruby,
''and'' if you are
inside an SQL statement.
Craig