Forum: Ruby on Rails RESTful nested resources and polymorphism?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Neubyr N. (Guest)
on 2009-03-08 04:02
With ref to my previous post: 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.
Neubyr N. (Guest)
on 2009-03-08 04:45
I think I mean to say self-referential rather than polymorphism..

Sorry..

CS.

Carlos S. wrote:
> With ref to my previous post: 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.
Neubyr N. (Guest)
on 2009-03-09 19:14
Bump up..
This topic is locked and can not be replied to.