http://railscasts.com/episodes/198-edit-multiple-individually
2010/7/31 Magnus Holm <judofyr-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
> Hey rails-talk, I have a little question: How do you handle multiple
> inserts like these in ActiveRecord? It''s for a little Camping app
that
> skylerrichter is writing, but it''s just plain, simple
ActiveRecord, so
> it shouldn''t really be any difference from a Rails app:
>
> @company = Company.create(
> :name => @input.name,
> :sub_domain => @input.subdomain)
>
> # Create the first user:
> @user = User.create(
> :company_id => @company.id,
> :first_name => @input.first_name,
> :last_name => @input.last_name,
> :email => @input.email,
> :password => @input.password)
>
> Both Company and User has validations, so there''s a possibility
that
> they don''t actually get saved to the DB, and in that case I
don''t want
> *any* of them to be saved. I was thinking about something like this:
>
> begin
> Company.transaction do
> @company = Company.create!(
> :name => @input.name,
> :sub_domain => @input.subdomain)
>
> @user = User.create!(
> :company_id => @company.id,
> :first_name => @input.first_name,
> :last_name => @input.last_name,
> :email => @input.email,
> :password => @input.password)
> end
> rescue
> @errors = [@company, @user].compact.map(&:full_messages).flatten
> render :errors
> else
> redirect Login
> end
>
> But I''m wondering if there''s a better (more Rails-y) way
to solve this?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
Rodrigo Mendonça
(62) 8567-3142
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.