# Polymorphic problems

**URL:** <https://discuss.rubyonrails.org/t/polymorphic-problems/35340>\
**Category:** rubyonrails-talk\
**Created:** [October 14, 2008, 9:40am UTC](https://discuss.rubyonrails.org/t/polymorphic-problems/35340 "2008-10-14T09:40:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [October 14, 2008, 9:40am UTC](https://discuss.rubyonrails.org/t/polymorphic-problems/35340/1 "2008-10-14T09:40:07Z")

</div>

Hi!

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

&nbsp;&nbsp;map.resources :articles do |articles| &nbsp;&nbsp;&nbsp;&nbsp;articles.resources :comments &nbsp;&nbsp;end

&nbsp;&nbsp;map.resources :posts do |posts| &nbsp;&nbsp;&nbsp;&nbsp;posts.resources :comments &nbsp;&nbsp;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:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [October 14, 2008, 1:00pm UTC](https://discuss.rubyonrails.org/t/polymorphic-problems/35340/2 "2008-10-14T13:00:00Z")

</div>

Serafino Picozzi wrote:

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

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

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [October 14, 2008, 1:18pm UTC](https://discuss.rubyonrails.org/t/polymorphic-problems/35340/3 "2008-10-14T13:18:45Z")

</div>

Pokkai Dokkai 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:** ![Jeff1](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/jeff1/32/1170_2.png) [@Jeff1](https://discuss.rubyonrails.org/u/Jeff1)\
**Post date:** [October 14, 2008, 5:05pm UTC](https://discuss.rubyonrails.org/t/polymorphic-problems/35340/4 "2008-10-14T17:05:37Z")

</div>

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 &nbsp;&nbsp;@parent = Article.find\_by\_id(params[:article\_id]) || Post.find(params[:post\_id] end

def ensure\_valid\_comment &nbsp;&nbsp;return unless params[:id] &nbsp;&nbsp;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 softiesonrails.com
