# Nested resources and controllers: I think I'm down the wrong path

**URL:** <https://www.ruby-forum.com/t/nested-resources-and-controllers-i-think-im-down-the-wrong-path/187597>\
**Category:** Rails\
**Created:** [April 26, 2010, 4:31pm UTC](https://www.ruby-forum.com/t/nested-resources-and-controllers-i-think-im-down-the-wrong-path/187597 "2010-04-26T16:31:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 26, 2010, 4:31pm UTC](https://www.ruby-forum.com/t/nested-resources-and-controllers-i-think-im-down-the-wrong-path/187597/1 "2010-04-26T16:31:03Z")

</div>

Hi,

I’m new to nested resources. I’m surprised to find that to make my  
code work I need to retrieve the parent model in my RESTful  
controllers for my dependencies.

So, for example, let’s say A has\_one B. In controller B I seem to need  
to refer to parent A, for example:

@b = A.find(params[:a\_id]).b

But this doesn’t seem right and this change has only seemed to become  
necessary as I’ve lately been using nested routes and nested form\_for  
stuff.

Can anyone confirm that using nested routes should not, in fact,  
require wholesale changes like the one above to dependency  
controllers?

Thanks,

Grar

---

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 26, 2010, 4:35pm UTC](https://www.ruby-forum.com/t/nested-resources-and-controllers-i-think-im-down-the-wrong-path/187597/2 "2010-04-26T16:35:05Z")

</div>

Grary S. wrote:

> Hi,
> 
> I’m new to nested resources. I’m surprised to find that to make my  
> code work I need to retrieve the parent model in my RESTful  
> controllers for my dependencies.
> 
> So, for example, let’s say A has\_one B. In controller B I seem to need  
> to refer to parent A, for example:
> 
> @b = A.find(params[:a\_id]).b
> 
> But this doesn’t seem right

No, it doesn’t. You should always be able to do B.find(params[:b\_id]) .  
What happens when you try?

## Best,

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

---

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 26, 2010, 4:59pm UTC](https://www.ruby-forum.com/t/nested-resources-and-controllers-i-think-im-down-the-wrong-path/187597/3 "2010-04-26T16:59:44Z")

</div>

Marnen,

Here is the expression of a representative model association in my  
project:

Parent … has\_one :dependent  
Dependent … belongs\_to :parent

Here is my complementing route:

map.resources :parents, :has\_one =\> [:dependent, :dependent\_2, …

Here is a change I’d lately made to a \_form.html.erb partial in views/  
children to make things (kind of) work:

\<% form\_for([:parent, @child], :url =\> { :action=\>:create }) do |f| %\>

Man, I’m vexed!

I feel like I need to understand how form\_for must be adapted for use  
with nested resources.

Grar

---

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 26, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/nested-resources-and-controllers-i-think-im-down-the-wrong-path/187597/4 "2010-04-26T17:32:32Z")

</div>

I can’t recreate the problem, somehow, let’s consider the question  
dead until I can.

Thanks,

Grar

---

<div class="post-metadata">

**Author:** ![grar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cc9497/32.png) [@grar](https://www.ruby-forum.com/u/grar)\
**Post date:** [April 26, 2010, 5:06pm UTC](https://www.ruby-forum.com/t/nested-resources-and-controllers-i-think-im-down-the-wrong-path/187597/5 "2010-04-26T17:06:56Z")

</div>

[Please quote when replying.]

Grary S. wrote:

> Marnen,
> 
> Here is the expression of a representative model association in my  
> project:
> 
> Parent … has\_one :dependent  
> Dependent … belongs\_to :parent
> 
> Here is my complementing route:
> 
> map.resources :parents, :has\_one =\> [:dependent, :dependent\_2, …

Why the :dependent\_2 ? That doesn’t match your associations above.

And do you really want has\_one? It has legitimate uses, but n most  
cases, you should combine the two models instead of using has\_one .

> Here is a change I’d lately made to a \_form.html.erb partial in views/  
> children to make things (kind of) work:
> 
> \<% form\_for([:parent, @child], :url =\> { :action=\>:create }) do |f| %\>
> 
> Man, I’m vexed!

Perhaps if you were to answer the question I asked, I could help you  
better.

Perhaps if you could say what you had already tried, and how it failed,  
we could help you better.

But with the information you are providing, and your failure to answer  
questions intended to gather additional information about the problem,  
it is nearly impossible to help.

> I feel like I need to understand how form\_for must be adapted for use  
> with nested resources.
> 
> Grar

## Best,

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