# Polymorphic problems

**URL:** <https://www.ruby-forum.com/t/polymorphic-problems/150866>\
**Category:** Rails\
**Created:** [October 14, 2008, 11:40am UTC](https://www.ruby-forum.com/t/polymorphic-problems/150866 "2008-10-14T11:40:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![serpico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@serpico](https://www.ruby-forum.com/u/serpico)\
**Post date:** [October 14, 2008, 11:40am UTC](https://www.ruby-forum.com/t/polymorphic-problems/150866/1 "2008-10-14T11:40:07Z")

</div>

Hi!

I have a problem related to polymorphic associations.  
Currently in my routes i define the following:

map.resources :articles do |articles|  
articles.resources :comments  
end

map.resources :posts do |posts|  
posts.resources :comments  
end

I have basically posts and articles models which relate to the  
polymorphic model comment.  
Routes are generated as I expect to, but the problem resides in the  
comments\_controller, since in this controller I’m not able to figure out  
if I’m creating a comment for a post or for an article.

Is there a (possibly clean) way to solve this?

Thanks

---

<div class="post-metadata">

**Author:** ![serpico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@serpico](https://www.ruby-forum.com/u/serpico)\
**Post date:** [October 14, 2008, 3:00pm UTC](https://www.ruby-forum.com/t/polymorphic-problems/150866/2 "2008-10-14T15:00:00Z")

</div>

Serafino P. wrote:

> Hi!
> 
> I have a problem related to polymorphic associations.  
> Currently in my routes i define the following:
> 
> map.resources :articles do |articles|  
> articles.resources :comments  
> end
> 
> map.resources :posts do |posts|  
> posts.resources :comments  
> end

try this  
map.resources :articles ,:has\_many=\>:comments  
map.resources :posts ,:has\_many=\>:comments

---

<div class="post-metadata">

**Author:** ![serpico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@serpico](https://www.ruby-forum.com/u/serpico)\
**Post date:** [October 14, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/polymorphic-problems/150866/3 "2008-10-14T15:18:45Z")

</div>

Pokkai D. wrote:

> try this  
> map.resources :articles ,:has\_many=\>:comments  
> map.resources :posts ,:has\_many=\>:comments

This will only shorten the code in routes, but not solve my problem.  
Thanks for the tip in any case

---

<div class="post-metadata">

**Author:** ![serpico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@serpico](https://www.ruby-forum.com/u/serpico)\
**Post date:** [October 14, 2008, 7:06pm UTC](https://www.ruby-forum.com/t/polymorphic-problems/150866/4 "2008-10-14T19:06:40Z")

</div>

On Oct 14, 4:40 am, Serafino P. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> ```
> posts.resources :comments
> 
> ```
> 
> end
> 
> I have basically posts and articles models which relate to the  
> polymorphic model comment.  
> Routes are generated as I expect to, but the problem resides in the  
> comments\_controller, since in this controller I’m not able to figure out  
> if I’m creating a comment for a post or for an article.
> 
> Is there a (possibly clean) way to solve this?

You can check for the presence of params[:post\_id] or  
params[:article\_id] to discover the route that got triggered.

I generally use a before\_filter:

class CommentsController \< ApplicationController

before\_filter :determine\_parent  
before\_filter :ensure\_valid\_comment

def determine\_parent  
@parent = Article.find\_by\_id(params[:article\_id]) ||  
Post.find(params[:post\_id]  
end

def ensure\_valid\_comment  
return unless params[:id]  
raise ActiveRecord::RecordNotFound unless  
@parent.comments.find(params[:id])  
end

end

This will first look for an article, and if that fails (returns nil),  
it will assume it’s a post. Raises an exception of neither are  
found. The second filter helps ensure that someone didn’t mess with  
the url.

Does this help?

Jeff  
[purpleworkshops.com](http://purpleworkshops.com)

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software
