# Restful routes in a polymorphic association

**URL:** <https://www.ruby-forum.com/t/restful-routes-in-a-polymorphic-association/87044>\
**Category:** Rails\
**Created:** [February 27, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/restful-routes-in-a-polymorphic-association/87044 "2007-02-27T20:29:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![giuseppe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/48db29/32.png) [@giuseppe](https://www.ruby-forum.com/u/giuseppe)\
**Post date:** [February 27, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/restful-routes-in-a-polymorphic-association/87044/1 "2007-02-27T20:29:20Z")

</div>

Hello,  
I have an Entry model with polymorphic associations with all kinds of  
contents, like notes, addresses, etc.

Class Entry \< ActiveRecord::Base  
belongs\_to :content, :polymorphic =\> true  
end

Class Note \< ActiveRecord::Base  
has\_one :entry, :as =\> :content  
end

Class Address \< ActiveRecord::Base  
has\_one :entry, :as =\> :content  
end

Now, I am trying to follow the RESTful path, so in routes.rb I have:  
map.resources :entries, :notes, :addresses, …

I have an index action in entries\_controller that retrieves all  
@entries and, finally, index.rhtml has the following:

\<% for entry in @entries %\> \<% end %\>

| Type | Title |
| --- | --- |
| \<%= entry.content\_type %\> | \<%= link\_to entry.content.title, entry\_path(entry) %\> |

BUT–here’s the thing. I DON’T really want entry\_path to generate the  
linked URL. Rather, I would very much like each individual entry’s  
content type to determine routing. Thus, for example, if  
entry.content\_type is “Note”, then I would like notes\_controller  
actions to be invoked, as if I had written the following:  
\<%= link\_to entry.content.title, note\_path(entry.content\_id) %\>

Does this make sense? At the very least I am sure the question is  
going to give away my poor knowledge of Ruby…

Many thanks in advance,  
Giuseppe B.

---

<div class="post-metadata">

**Author:** ![giuseppe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/48db29/32.png) [@giuseppe](https://www.ruby-forum.com/u/giuseppe)\
**Post date:** [February 27, 2007, 9:24pm UTC](https://www.ruby-forum.com/t/restful-routes-in-a-polymorphic-association/87044/2 "2007-02-27T21:24:32Z")

</div>

…I mean, of course I could simply do:

\<%= link\_to entry.content.title, "/notes/#{entry.content\_id} %\>

but I’d rather not, of course…

I guess my question may boil down to the following.  
Is it possible in Ruby to place a method call by putting together a  
string at runtime and having it executed?  
In this particular case the string might look like:  
s = “#{entry.content\_type}\_path(#{entry.content\_id})”

Thanks again in advance  
Giuseppe

---

<div class="post-metadata">

**Author:** ![giuseppe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/48db29/32.png) [@giuseppe](https://www.ruby-forum.com/u/giuseppe)\
**Post date:** [March 2, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/restful-routes-in-a-polymorphic-association/87044/3 "2007-03-02T18:22:55Z")

</div>

Giuseppe írta:

> s = “#{entry.content\_type}\_path(#{entry.content\_id})”

sure:  
s = eval("#{entry.content\_type}\_path(#{entry.content\_id})")

> Thanks again in advance  
> Giuseppe
> 
> >

–  
Parragh S.  
e-mail: removed\_email\_address@domain.invalid  
web: parszab.nir.hu
