# Noob question on associated records and their controller structure

**URL:** <https://www.ruby-forum.com/t/noob-question-on-associated-records-and-their-controller-structure/184816>\
**Category:** Rails\
**Created:** [March 9, 2010, 2:03am UTC](https://www.ruby-forum.com/t/noob-question-on-associated-records-and-their-controller-structure/184816 "2010-03-09T02:03:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/91b2a8/32.png) [@Grary](https://www.ruby-forum.com/u/Grary)\
**Post date:** [March 9, 2010, 2:03am UTC](https://www.ruby-forum.com/t/noob-question-on-associated-records-and-their-controller-structure/184816/1 "2010-03-09T02:03:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Grary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/91b2a8/32.png) [@Grary](https://www.ruby-forum.com/u/Grary)\
**Post date:** [March 9, 2010, 6:51pm UTC](https://www.ruby-forum.com/t/noob-question-on-associated-records-and-their-controller-structure/184816/2 "2010-03-09T18:51:40Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Grary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/91b2a8/32.png) [@Grary](https://www.ruby-forum.com/u/Grary)\
**Post date:** [March 9, 2010, 6:53pm UTC](https://www.ruby-forum.com/t/noob-question-on-associated-records-and-their-controller-structure/184816/3 "2010-03-09T18:53:29Z")

</div>

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.

> Grary

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Grary](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/91b2a8/32.png) [@Grary](https://www.ruby-forum.com/u/Grary)\
**Post date:** [March 9, 2010, 6:55pm UTC](https://www.ruby-forum.com/t/noob-question-on-associated-records-and-their-controller-structure/184816/4 "2010-03-09T18:55:00Z")

</div>

You should just be able to do child.parent

–  
Jeremy C.  
[http://twitter.com/jeremychase](http://twitter.com/jeremychase)

On Tue, Mar 9, 2010 at 12:51 PM, Marnen Laibow-Koser
