# RESTful nested resources and polymorphism?

**URL:** <https://www.ruby-forum.com/t/restful-nested-resources-and-polymorphism/162287>\
**Category:** Rails\
**Created:** [March 8, 2009, 3:02am UTC](https://www.ruby-forum.com/t/restful-nested-resources-and-polymorphism/162287 "2009-03-08T03:02:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 8, 2009, 3:02am UTC](https://www.ruby-forum.com/t/restful-nested-resources-and-polymorphism/162287/1 "2009-03-08T03:02:48Z")

</div>

With ref to my previous post: [RESTful app and refactoring code in different controllers - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/180356)  
I am now able to access items from topics controller. However, now I  
need to add sub-item e.g.: An item itself could have many sub-items. So  
my items model is like:  
class Item \< ActiveRecord::Base  
validates\_uniqueness\_of :title, :scope =\> [:topic\_id]  
validates\_presence\_of :topic\_id, :title, :owner, :position

belongs\_to :topic  
belongs\_to :parent,  
:class\_name =\> “Item”,  
:foreign\_key =\> “parent\_id”  
has\_many :children,  
:class\_name =\> “Item”,  
:foreign\_key =\> “parent\_id”,  
:order =\> :position,  
:dependent =\> :destroy #don’t leave orphans (cascade delete)  
has\_many :attachments,  
:dependent =\> :destroy #don’t leave orphans (cascade delete)  
end

My config/routes.rb file is:  
map.resources :topics do |topics|  
topics.resources :items do |items|  
items.resources :attachments  
end  
end

Now, if I need to create a new subitem then what changes should I make  
to my routing file? And what will be the form of RESTful urls?

Thanks,  
CS.

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 9, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/restful-nested-resources-and-polymorphism/162287/2 "2009-03-09T18:14:25Z")

</div>

Bump up…

---

<div class="post-metadata">

**Author:** ![neubyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c37758/32.png) [@neubyr](https://www.ruby-forum.com/u/neubyr)\
**Post date:** [March 8, 2009, 3:45am UTC](https://www.ruby-forum.com/t/restful-nested-resources-and-polymorphism/162287/3 "2009-03-08T03:45:09Z")

</div>

I think I mean to say self-referential rather than polymorphism…

Sorry…

CS.

Carlos S. wrote:

> With ref to my previous post: [RESTful app and refactoring code in different controllers - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/180356)  
> I am now able to access items from topics controller. However, now I  
> need to add sub-item e.g.: An item itself could have many sub-items. So  
> my items model is like:  
> class Item \< ActiveRecord::Base  
> validates\_uniqueness\_of :title, :scope =\> [:topic\_id]  
> validates\_presence\_of :topic\_id, :title, :owner, :position
> 
> belongs\_to :topic  
> belongs\_to :parent,  
> :class\_name =\> “Item”,  
> :foreign\_key =\> “parent\_id”  
> has\_many :children,  
> :class\_name =\> “Item”,  
> :foreign\_key =\> “parent\_id”,  
> :order =\> :position,  
> :dependent =\> :destroy #don’t leave orphans (cascade delete)  
> has\_many :attachments,  
> :dependent =\> :destroy #don’t leave orphans (cascade delete)  
> end
> 
> My config/routes.rb file is:  
> map.resources :topics do |topics|  
> topics.resources :items do |items|  
> items.resources :attachments  
> end  
> end
> 
> Now, if I need to create a new subitem then what changes should I make  
> to my routing file? And what will be the form of RESTful urls?
> 
> Thanks,  
> CS.
