I''ve run into a bit of a problem with saving 2 related, but different, object records at once: When a user registers, they specify the name of the organization they belong to. Upon a successful registration, a new user record and a new organization record is created. However, I can''t figure out how to only create the organization if the user record is validated. Specifically, the organization name is not an attribute of the user, so I cannot retrieve the organization name using user.organzation_name in the after_create callback in the user model. Any ideas on how to create one record, and then create another object that is related to that record? Now that sounded confusing... - Derek -- Derek Haynes HighGroove Studios - http://www.highgroove.com Keeping it Simple. 404.593.4879
I do this sort of thing in the app I''m working on now, assuming I 
understood you correctly. (And I probably did. Heh)
Caveat: I''m a relative Rails newbie, so don''t do an IPO based
on this
e-mail.
def destroy_spacetime
   unless params[:blackhole] then
     redirect_to :action => ''somewhere''
     return
   end
   @black_hole = session[:event_horizon] || Anomaly.new
   @black_hole.attributes = params[:blackhole]
   @universe = Universe.new
   @universe.attributes = params[:universe]
   if @black_hole.valid? && @universe.valid? then
     @black_hole.save
     @universe.save
   else
     redirect_to :action => ''where_we_came_from''
     return
   end
   session[:event_horizon] = @black_hole
end
Derek Haynes wrote:> I''ve run into a bit of a problem with saving 2 related, but
different,
> object records at once:
> 
> When a user registers, they specify the name of the organization they
> belong to.
> 
> Upon a successful registration, a new user record and a new
> organization record is created.
> 
> However, I can''t figure out how to only create the organization if
the
> user record is validated.
> 
> Specifically, the organization name is not an attribute of the user,
> so I cannot retrieve the organization name using user.organzation_name
> in the after_create callback in the user model.
> 
> Any ideas on how to create one record, and then create another object
> that is related to that record?
> 
> Now that sounded confusing...
>
On 7/23/05, Derek Haynes <derek.haynes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve run into a bit of a problem with saving 2 related, but different, > object records at once: > > When a user registers, they specify the name of the organization they > belong to. > > Upon a successful registration, a new user record and a new > organization record is created. > > However, I can''t figure out how to only create the organization if the > user record is validated.Use transactions and save! User.transaction do @organisation = Organisation.new(params[:organisation]) @user = User.new(params[:user]) @organisation.save! @user.organisation=@organisation @user.save! end> Specifically, the organization name is not an attribute of the user, > so I cannot retrieve the organization name using user.organzation_name > in the after_create callback in the user model. > > Any ideas on how to create one record, and then create another object > that is related to that record? > > Now that sounded confusing... > > - Derek > > > -- > Derek Haynes > HighGroove Studios - http://www.highgroove.com > Keeping it Simple. > 404.593.4879 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz