# Restful Nested Resource without ugly id

**URL:** <https://www.ruby-forum.com/t/restful-nested-resource-without-ugly-id/122422>\
**Category:** Rails\
**Created:** [December 7, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/restful-nested-resource-without-ugly-id/122422 "2007-12-07T14:50:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kei\_Simone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@Kei\_Simone](https://www.ruby-forum.com/u/Kei_Simone)\
**Post date:** [December 7, 2007, 2:50pm UTC](https://www.ruby-forum.com/t/restful-nested-resource-without-ugly-id/122422/1 "2007-12-07T14:50:31Z")

</div>

To all:

hi i am pretty noob. Basically I am learning via copy and paste, so  
please forgive me.

I am using rails 1.2.6.

I built 2 scaffold\_resource called traders and orders.

I want a localhost:3000/v1/traders/{trader\_name}/orders

so I did a

base = ‘/v1’

map.resources :traders, :path\_prefix =\> base

#trader\_url  
trader\_url = base + ‘/traders/:login’

map.resources :orders, :path\_prefix =\> trader\_url

inside the orders\_controller:

i did this  
def index

```
@orders = Order.find_by_trader_id(@trader.id)
respond_to do |format|

   format.html
end

```

I have 2 questions:

1. am i doing it right in the routes.rb?

2. i get an error at the webpage.

undefined method `each’ for #Order:0x4624f18

Extracted source (around line #7):

4:

  
5:   
6:  
7: \<% for order in @orders %\>  
8:  
9:   
10: \<% for column in Order.content\_columns %\>

what is wrong?

Please help. I thank you all.

---

<div class="post-metadata">

**Author:** ![Kei\_Simone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@Kei\_Simone](https://www.ruby-forum.com/u/Kei_Simone)\
**Post date:** [December 7, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/restful-nested-resource-without-ugly-id/122422/2 "2007-12-07T15:26:50Z")

</div>

> ```
> @orders = Order.find_by_trader_id(@trader.id)
> 
> ```

this will find only one order, so there’s no ‘each’

otherwise your view code would be ok

@orders = Order.find\_all\_by\_trader\_id(@trader.id)

would do what you want

but it would be better (more rails like) to define  
has\_many :orders  
in Trader Model and  
belongs\_to :trader  
in Order model

then you could use

@trader.orders.each do |order|

in the view

---

<div class="post-metadata">

**Author:** ![Kei\_Simone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@Kei\_Simone](https://www.ruby-forum.com/u/Kei_Simone)\
**Post date:** [December 7, 2007, 4:05pm UTC](https://www.ruby-forum.com/t/restful-nested-resource-without-ugly-id/122422/3 "2007-12-07T16:05:05Z")

</div>

Thank you.

However, is that a good idea about using the @trader.orders.each do |  
order|  
considering that it appears less MVC?

What about my routes.rb? Any room for improvement?

On Dec 7, 10:26 pm, Thorsten M. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![Kei\_Simone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@Kei\_Simone](https://www.ruby-forum.com/u/Kei_Simone)\
**Post date:** [December 7, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/restful-nested-resource-without-ugly-id/122422/4 "2007-12-07T16:17:52Z")

</div>

> However, is that a good idea about using the @trader.orders.each do |  
> order|  
> considering that it appears less MVC?

just an example or option. after all it does, what you do anyway and it  
keeps things easy readable  
you could of course make use of partials and hand over @trader.orders as  
an collection:  
render :partial =\> ‘order\_list’, :collection =\> @trader.orders

but if there’s a question between MVC and Rails, i go for rails 🙂

> What about my routes.rb? Any room for improvement?  
> seems ok to me, but i’m not that much an expert in the routing stuff

maybe nested routing would be an option:

map.resources :traders do |trader|  
trader.resources :orders  
end

would give you nice path helpers like:

trader\_orders\_path(trader\_id)

to get the full path to the index of orders for a trader with  
params[:trader\_id] filled as you expect it

---

<div class="post-metadata">

**Author:** ![Kei\_Simone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@Kei\_Simone](https://www.ruby-forum.com/u/Kei_Simone)\
**Post date:** [December 7, 2007, 5:42pm UTC](https://www.ruby-forum.com/t/restful-nested-resource-without-ugly-id/122422/5 "2007-12-07T17:42:07Z")

</div>

> i googled around and i know abt the map.resources code block that you  
> showed me.
> 
> however, that would give me urls like /traders/2/orders/2
> 
> instead of traders/peter/orders/2
> 
> is there an alternative to the one that I wrote and still give me what  
> i want?

sure, just overwrite to\_param in Trader model

def to\_param  
name  
end

this would give eg ‘peter’ for trader\_orders\_path(@trader) or  
trader\_order\_path(@trader, 2) for your example  
would be better to have an url\_name col for that, to make sure the name  
can be used in an url (no spaces etc) or to return an encoded form of  
name

then search your trader by the name, handed back in params[:trader\_id]

---

<div class="post-metadata">

**Author:** ![Kei\_Simone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@Kei\_Simone](https://www.ruby-forum.com/u/Kei_Simone)\
**Post date:** [December 7, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/restful-nested-resource-without-ugly-id/122422/6 "2007-12-07T18:01:58Z")

</div>

In order to do what you want to do you will have to do two things:

1, override the to\_param method on your models

Assuming you have a “name” field in your Trader model, in the  
Trader.rbfile, you’d add this:

def to\_param  
self.name  
end

That’ll make your URLs work as long as you’re always passing the @trader  
object around. However, you have to change all of your finders now to  
look  
up by name

So, instead of

@trader = Trader.find(params[:id])

You must do  
@trader = Trader.find\_by\_name(params[:id])

Repeat for the user model and controllers.

If you’re just starting out, don’t mess with this now. Learn how routing  
and  
finders actually work. You may discover a better way too. The ids  
don’t  
hurt anything and are much less volatile. If you change the trader’s  
name  
for example, the link is no longer valid.

Also, scaffolding isn’t meant for production. The URL stuff you’re  
trying  
to do is infinitely easier to do if you don’t use scaffolding. That’s  
why I  
say don’t tackle this now, wait until you understand how everything  
works  
together. Once you get it, these types of URLs become trivial because  
you’re WRITING the finders yourself, instead of changing a bunch that  
were  
originally intended to work a different way.

Best of luck!

---

<div class="post-metadata">

**Author:** ![Kei\_Simone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@Kei\_Simone](https://www.ruby-forum.com/u/Kei_Simone)\
**Post date:** [December 8, 2007, 2:14am UTC](https://www.ruby-forum.com/t/restful-nested-resource-without-ugly-id/122422/7 "2007-12-08T02:14:11Z")

</div>

> Learn how routing and  
> finders actually work.

I am currently learning by playing around with the scaffolds. Do you  
have certain resources to point me to? Bear in mind, I am pretty damn  
noob.

> Also, scaffolding isn’t meant for production.

Oh no, I want to do one for a very simple game for production. If I  
dont use scaffold, i may be lost. Even if i code from scratch, i will  
inevitably copy teh code from scaffold.

I do have the eBook of Agile Devt with Rails and a few other Rails  
eBook. But sometimes I feel the examples they use are a tad too  
simple. Furthermore, i intend to use make a more Restful application.

Please help. THanks

---

<div class="post-metadata">

**Author:** ![Kei\_Simone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/839c29/32.png) [@Kei\_Simone](https://www.ruby-forum.com/u/Kei_Simone)\
**Post date:** [December 7, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/restful-nested-resource-without-ugly-id/122422/8 "2007-12-07T16:30:22Z")

</div>

> map.resources :traders do |trader|  
> trader.resources :orders  
> end
> 
> would give you nice path helpers like:
> 
> trader\_orders\_path(trader\_id)

i googled around and i know abt the map.resources code block that you  
showed me.

however, that would give me urls like /traders/2/orders/2

instead of traders/peter/orders/2

is there an alternative to the one that I wrote and still give me what  
i want?

thank you.
