# More Nested Resources Questions

**URL:** <https://www.ruby-forum.com/t/more-nested-resources-questions/135660>\
**Category:** Rails\
**Created:** [April 23, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/more-nested-resources-questions/135660 "2008-04-23T22:53:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [April 23, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/more-nested-resources-questions/135660/1 "2008-04-23T22:53:49Z")

</div>

Ok… quick example…

client has\_many :locations  
vendor has\_many :locations

so my resources is something like…

map.resources :clients, :has\_many =\> :locations  
map.resources :vendors, :has\_many =\> :locations  
map.resources :locations

Now, my new location view’s form looks like this…

form\_for( [@client, @location] ) do |f|

N.B.: I have a before\_filter in my Location controller that sets  
@client = Client.find(params[:client\_id])

the problem is, what if the new location is supposed to belong to a  
vendor? Do I need to have a separate resource for the two different  
types of resources? What’s the best way to go about tackling this  
issue?

I swear once I get this all figured out I’m gonna write up a nice  
little write up for google to catch 🙂

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [April 24, 2008, 8:05am UTC](https://www.ruby-forum.com/t/more-nested-resources-questions/135660/2 "2008-04-24T08:05:13Z")

</div>

No clue?

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [April 24, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/more-nested-resources-questions/135660/3 "2008-04-24T16:23:41Z")

</div>

It’s scary how little information there is on RESTful development in  
Rails, yet it’s supposed to be “the way” to program. ☹

I’ve been reading all night and I can’t figure this one out. can  
anyone point me to a direction that might be able to help.

I apologize if this is un-necessary “spam” or “bumping”

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [April 24, 2008, 6:26pm UTC](https://www.ruby-forum.com/t/more-nested-resources-questions/135660/4 "2008-04-24T18:26:30Z")

</div>

I am going to assume that you’re using a polymorphic association on  
location, something like:

class Location \< ARec::Base  
belongs\_to :locatable, :polymorphic=\>true  
end

class Vendor (or Client) \< ARec::Base  
has\_many :locations, :as=\>:locatable  
end

The polymorphic association gives you the clue. Instead of finding a  
@client or a @vendor, find a @locatable

locations\_controller.rb  
…  
def new  
@locatable = params[:vendor\_id] ? Vendor.find(params[:vendor\_id]) :  
nil  
@locatable ||= Client.find(params[:client\_id]) if params[:client\_id]  
@location = @locatable.locations.build  
end  
…

locations/new.html.erb  
form\_for( [@locatable, @location] ) do |f|  
…  
end

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [April 24, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/more-nested-resources-questions/135660/5 "2008-04-24T18:49:06Z")

</div>

Duh,  
OK so clients and vendor have many locations (seems like they would  
have one)

map.resources :clients do |client|  
client.resources :locations  
end

map.resources :vendors do |vendor|  
vendor.resources :locations  
end

This should give you vendor and client routes like:  
vendors/3/location/2  
clients/5/location/2

BUT… Maybe you learned the difference between singular and plural  
RESTful routes.

Teedub

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [April 24, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/more-nested-resources-questions/135660/6 "2008-04-24T18:45:34Z")

</div>

check out  
[http://api.rubyonrails.org/classes/ActionController/Resources.html](http://api.rubyonrails.org/classes/ActionController/Resources.html)  
Disclaimer:  
I too am still learning RESTful routes, which is pretty involved.

But I think you need to nest the routes as each Client and vendor will  
have only one location.

like

map.resources :clients do |client|  
client.resource :location  
end

map.resources :vendors do |vendor|  
vendor.resource :location  
end

This should give you vendor and client routes like:  
vendors/3/location  
clients/5/location

Note that clients and vendors are plural and thus use map.resources  
(plural)  
and that location is singular and the receiver of the singular  
‘resource’ message is a vendor object, not a map.

Good reading on the subject can be found in David A Black’s “Rails  
Routing”, which is a bit outdated, but gives you a good fundamental  
understanding of RESTful Routes. I think it is still well worth the 15  
bucks as David has a wonderful gift of presenting complex material in  
a manner in which mere mortals can understand.

> **[Rails Routing (Digital Shortcut) | InformIT](https://www.informit.com/store/rails-routing-digital-shortcut-9780321509246)**
>
> The Rails routing system has always been a key component of theframework, and with the introduction of RESTful routes in the pastyear, it has taken center stage. Fully programmable, the routing systemgoverns the process of mapping request URLs to the...

On Apr 24, 7:22 am, sw0rdfish [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Ok… quick example…  
> > Now, my new location view’s form looks like this…
> 
> > I swear once I get this all figured out I’m gonna write up a nice

little write up for google to catch 🙂

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [April 24, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/more-nested-resources-questions/135660/7 "2008-04-24T19:17:20Z")

</div>

The more I think about it, the form\_for thing isn’t really an  
issue… since one would be coming from the Client’s Views, and one  
would be coming from the Vendor’s view…

Then based on the view it came from, ( IE is @vendor or @client set )  
redirect back to the correct SHOW action in the correct controller…

---

<div class="post-metadata">

**Author:** ![sw0rdfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/edb3f5/32.png) [@sw0rdfish](https://www.ruby-forum.com/u/sw0rdfish)\
**Post date:** [April 24, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/more-nested-resources-questions/135660/8 "2008-04-24T19:04:22Z")

</div>

Thanks Andy, I’ll check that out… that may be what I’m looking for…

Frank, I understand the concepts you’re outining, my issues arise when  
I try to redirect after adding a location for example… do I go to  
show a client or do I go to show a vendor? And no, they can have many  
locations for example if my client is Walmart, they have many  
locations that I want to know of…

Where it breaks down for me is with the forms for and the redirects  
after a CRUD call, because I don’t know how to tell the app to go to  
show the Client or the Vendor or whatever the location object belongs  
to. And passing @client, @location to the form\_for works, but what  
about the vendor form? I’d have to have some ugly if statements in my  
view… and what if location is associated with 10 different  
models… i’m hoping Andy’s advice helps.
