Say I have a model something like this Parent has_many kids Kid belongs_to Parent. Now I have view for say a new Parent. I also have provided an AJAX form so that they can fill in all the children they have as well. Whats the best way to go about adding a child to a parent at this stage. Nothing has been committed to the database. And I can''t add a child to the database without having a parent. But the parent has not been created yet. Thanks in advance for help! ^_^ -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Take a look at ActiveRecord transactions. If your params from the controller are :parent and :kids in your model method you would do a transaction: def self.create_family(params) Parent.transaction do parent = Parent.create(params[:parent]) params[:kids].each do |kid| parent.kids.create(kid) end end end If one of the creates fails, they all get rolled back. John. On Jan 3, 9:47 pm, Luke Grimstrup <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Say I have a model something like this > Parent has_many kids > Kid belongs_to Parent. > > Now I have view for say a new Parent. I also have provided an AJAX form > so that they can fill in all the children they have as well. > > Whats the best way to go about adding a child to a parent at this stage. > Nothing has been committed to the database. And I can''t add a child to > the database without having a parent. But the parent has not been > created yet. > > Thanks in advance for help! ^_^ > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Luke Grimstrup
2008-Jan-04 03:54 UTC
Re: Making an child before the parent is commited to DB
Cheers for that, I think that''ll work. Another question: I have bunch of text_fields tags that get pushed onto the form via Ajax every time the user hits "add new child" now, if I wanted to add 4 kids to this family. Then I would have 4 text_fields. Is it possible to group the text_fields in the view so that I can iterate through them in the code-behind? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Edwards
2008-Jan-04 12:25 UTC
Re: Making an child before the parent is commited to DB
Have you seen the Railscast on this subject, I think it more or less describes what you''re trying to achieve. http://www.railscasts.com/episodes/75 On 4 Jan, 03:54, Luke Grimstrup <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Cheers for that, I think that''ll work. > > Another question: > I have bunch of text_fields tags that get pushed onto the form via Ajax > every time the user hits "add new child" now, if I wanted to add 4 kids > to this family. Then I would have 4 text_fields. > > Is it possible to group the text_fields in the view so that I can > iterate through them in the code-behind? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Luke Grimstrup
2008-Jan-04 20:21 UTC
Re: Making an child before the parent is commited to DB
Thanks for sharing that wonderful resource. It looks exactly like the thing I''m after! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---