rest - nested resources

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 :slight_smile: Stuart

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.

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.

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 :slight_smile: Stuart

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 :slight_smile: 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 ............................

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

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 ............................

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| %>

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

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:

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