# Rest - nested resources

**URL:** <https://www.ruby-forum.com/t/rest-nested-resources/73207>\
**Category:** Rails\
**Created:** [October 22, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/rest-nested-resources/73207 "2006-10-22T21:59:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 22, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/rest-nested-resources/73207/1 "2006-10-22T21:59:00Z")

</div>

Hope this is going to come out understandable.  
Right now I have a nested resource within my main rest route - as in:

map.resources :candidates do |candidate|  
candidate.resources :canbackgrounds  
end

The reason I have this setup is because I’m working on a multi part form  
where I"m giving the user the option to do “sections” at their leisure,  
etc.

So the first section (the main - is candidates)  
the second section is canbackgrounds.

However within the actual canbackground form is a field that is inputted  
into another model / table.  
My question then is should I be adding this other model into the nested  
resources, as perhaps a nested resource within canbackgrounds ?

Hope this isn’t all too confusing , I know it is for me 🙂  
Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 23, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/rest-nested-resources/73207/2 "2006-10-23T18:03:37Z")

</div>

> So the first section (the main - is candidates)  
> the second section is canbackgrounds.
> 
> However within the actual canbackground form is a field that is inputted  
> into another model / table.  
> My question then is should I be adding this other model into the nested  
> resources, as perhaps a nested resource within canbackgrounds ?

Resources provide a way to create groups of related routes. So the  
question  
is do you need to manipulate this field? I don’t think it makes sense to  
define resource for just one field. You will be able to deal with your  
canbackground attributes in a RESTful way. It is not clear from your  
question why you want to nest another resource for only one field.

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 23, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/rest-nested-resources/73207/3 "2006-10-23T18:23:41Z")

</div>

On 10/23/06, Bala P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > resources, as perhaps a nested resource within canbackgrounds ?
> 
> Resources provide a way to create groups of related routes. So the  
> question is do you need to manipulate this field? I don’t think it makes  
> sense to define resource for just one field. You will be able to deal with  
> your canbackground attributes in a RESTful way. It is not clear from your  
> question why you want to nest another resource for only one field.

Bala , thank you for responding.  
What I’ve done or will be doing is defining 5 resources. 4 of which is  
nested in the first.  
However in a few of those 4 nested resources, the forms contain input  
elements for models not currently in those resources.  
Why do these models exist ? Because i have a number of form elements  
that  
are multiple selection lists.  
So to reference canbackgrounds -  
in the new form i have a select list , multiple, this is written to the  
canindustries model.  
My tables looks like this:  
(nested resource of candidates → canbackgrounds | id | candidate\_id |  
somefield

1 10 3

(not sure what to do with this → canindustries | canbackground\_id |  
candidate\_id | industry\_id

1 10 4

1 10 7

1 10 9

Hope this explains it better. Let me know what you think…anyone 🙂  
Stuart

> 

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 23, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/rest-nested-resources/73207/4 "2006-10-23T19:59:47Z")

</div>

On 10/23/06, Bala P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > 1 10 3
> > 
> > ```
> > 
> > Hope this explains it better. Let me know what you think…anyone 🙂  
> > Stuart
> 
> So you basically have values for multiple selection list that is part of  
> the canbackgrounds model. You want to know how to persist these values. Is  
> that your question?

Sorry Bala, I’m truly not trying to be confusing.  
No, those multiple selection values are going into ANOTHER model.

some code to demonstrate -

config/routes.rb

map.resources :candidates do |candidate|  
candidate.resources :canbackgrounds  
end

Now from teh canbackgrounds ‘new’ form  
\<% form\_for :canbackground, :url =\> canbackground\_url, :candidate\_id =\>  
@candidate\_id do |form| %\>  
…

\<%= text\_field(:canbackground, :recent\_title) %\> \<------------- going to model Canbackground \<---------- going to model Canindustry \<%= options\_from\_collection\_for\_select @categories, :id, :name %\> ............................

Now it’s all working fine as it’s currently set up, however maybe I’m  
not  
seeing a better way of doing it. As you said in the first message, for  
one  
field it probably doesn’t make sense to create a seperate resource,  
that’s  
kind of how I feel.  
Anyway, hopefully this explains it better.

Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 23, 2006, 8:25pm UTC](https://www.ruby-forum.com/t/rest-nested-resources/73207/5 "2006-10-23T20:25:53Z")

</div>

> …
> 
> Now it’s all working fine as it’s currently set up, however maybe I’m not  
> seeing a better way of doing it. As you said in the first message, for one  
> field it probably doesn’t make sense to create a seperate resource, that’s  
> kind of how I feel.  
> Anyway, hopefully this explains it better.

You can have multiple models in a form. The fields\_for helper can be  
used  
for this purpose. In this case you can have:

\<% form\_for :canbackground, :url =\> canbackground\_url, :candidate\_id =\>  
@candidate\_id do |form| %\>

\<%= form.text\_field (:canbackground, :recent\_title) %\>

\<% fields\_for :canindustries do |canindustry| %\>

\<%= canindustry.options\_from\_collection\_for\_select @categories, :id, :name %\> 

I don’t know if there is any other better way of doing this.

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 23, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/rest-nested-resources/73207/6 "2006-10-23T22:05:50Z")

</div>

On 10/23/06, Bala P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > \<%= options\_from\_collection\_for\_select @categories, :id, :name
> 
> \<%= canindustry.options\_from\_collection\_for\_select @categories, :id, :name  
> %\>
> 
> I don’t know if there is any other better way of doing this.

Bala, thank you for the help and responses. You helped clear up my  
concerns.

Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 24, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/rest-nested-resources/73207/7 "2006-10-24T14:17:20Z")

</div>

On 10/23/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > \<select name=canindustry[category\_id][] size="6"

Well , now I’m experiencing a problem with this. As for above  
new/create  
works fine.  
Edit though is a problem as the canindustry field in the edit form is  
not  
returning the previously selected records.  
Not sure how to fix this issue.

The edit form looks like this:  
\<% form\_for :canbackground,  
:url =\> canbackground\_url,  
:candidate\_id =\> @candidate\_id,  
:html =\> {:method =\> :put} do |form| %\>  
…fields…  
#in the middle is the canindustry field:  
  
\<%= options\_from\_collection\_for\_select @categories, :id, :name  
%\>

The controller has this line for returning teh previously selected  
fields  
for canbackground  
@canbackground = @candidate.canbackgrounds.find(params[:id])

However I have no clue how to pull up the previously stored values for  
canindustry.

Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 23, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/rest-nested-resources/73207/8 "2006-10-23T19:47:42Z")

</div>

So to reference canbackgrounds -

> 1 10 4
> 
> 1 10 7
> 
> 1 10 9
> 
> Hope this explains it better. Let me know what you think…anyone 🙂  
> Stuart

So you basically have values for multiple selection list that is part of  
the  
canbackgrounds model. You want to know how to persist these values. Is  
that  
your question?
