Grary
2010-Mar-09 01:02 UTC
noob question on associated records and their controller structure
Hi, I have controllers for two models between which there exists a belongs_to/has_one relationship. How do I reference the parent model object in the child controller so that I can later recover the child object in the parent controller, i.e., create and "attach" the child to the parent model for later recovery? I would have guessed it was something like as follows: class ChildController... def create @parent = ParentModel.find(params[:id]) # How to get "current" parent from database? @parent.child = ChildModel.new(params[:child]) ... Any thoughts? Grary -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Mar-09 17:51 UTC
Re: noob question on associated records and their controller
Grary wrote:> Hi, > > I have controllers for two models between which there exists a > belongs_to/has_one relationship. > > How do I reference the parent model object in the child controller so > that I can later recover the child object in the parent controller, > i.e., create and "attach" the child to the parent model for later > recovery?If you have your associations set up properly, this is automatic. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Mar-09 17:53 UTC
Re: noob question on associated records and their controller
Grary wrote:> Hi, > > I have controllers for two models between which there exists a > belongs_to/has_one relationship. > > How do I reference the parent model object in the child controller so > that I can later recover the child object in the parent controller, > i.e., create and "attach" the child to the parent model for later > recovery? > > I would have guessed it was something like as follows: > > class ChildController... > > def create > @parent = ParentModel.find(params[:id]) # How to get "current" > parent from database?Don''t bother finding the parent model if you already have the ID. That''s an extra query for no reason in most cases.> @parent.child = ChildModel.new(params[:child])Oh, on second reading, I see what you''re asking. Check out nested resources and accepts_nested_attributes_for. Also read the Rails guide on associations if you haven''t already.> ... > > Any thoughts?Yes, try it before you ask! :) Rails makes this simple.> > GraryBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jeremy Chase
2010-Mar-09 17:53 UTC
Re: Re: noob question on associated records and their controller
You should just be able to do child.parent -- Jeremy Chase http://twitter.com/jeremychase On Tue, Mar 9, 2010 at 12:51 PM, Marnen Laibow-Koser <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Grary wrote: > > Hi, > > > > I have controllers for two models between which there exists a > > belongs_to/has_one relationship. > > > > How do I reference the parent model object in the child controller so > > that I can later recover the child object in the parent controller, > > i.e., create and "attach" the child to the parent model for later > > recovery? > > If you have your associations set up properly, this is automatic. > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > 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-/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. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.