Nested resources with RESTful Rails

1 view
Skip to first unread message

sergi...@gmail.com

unread,
Oct 8, 2007, 10:21:32 AM10/8/07
to Ruby on Rails: Talk
Hi,

I'm using RESTful in Rails, and I have an application with the
following situation:

I have categories, articles and comments. A category can contain many
articles (one-to-many relation) and an article can have many comments
(one-to-many relation). This is like nested in nested resources. My
question is how do I treat this in RESTful?

BTW, I've already implemented the code for categories and articles, I
still have to add the comments and I'm not sure how. If I add
comments, I'll have to display the comments only inside the article
show view and there shouldn't be a view where I can only see some
comments. Also, how do I integrate the comments_controller::index
action with the articles show view?

Mike Perham

unread,
Oct 8, 2007, 10:38:11 AM10/8/07
to rubyonra...@googlegroups.com

I would not think of an article as a nested resource within a category.
I think of a nested resource as something that is tied to the lifecycle
of the parent object. If you delete the parent, you also would want to
delete the children. In this case, article would be the parent and
comments would be the children because if you delete an article, you
want to delete the comments. In the case of something like a blogging
system, this would not be true of the category/article relationship and
so I would not use a nested pattern there.

mike
--
Posted via http://www.ruby-forum.com/.

myst_tt

unread,
Oct 8, 2007, 11:47:05 AM10/8/07
to Ruby on Rails: Talk
Hi, I recommend you to consult this paper by Google - "RESTful Rails
Development"
On the second part, it shows a pretty detailed example how nested
resource could be handled probably - how to revise route.rb, etc.
Hope this could resolve your puzzle.

Br,
Xi


On 10月8日, 下午5时21分, "sergiutr...@gmail.com" <sergiutr...@gmail.com>
wrote:

sergi...@gmail.com

unread,
Oct 8, 2007, 12:10:28 PM10/8/07
to Ruby on Rails: Talk
myst_tt, you forgot to post the link. thanks :)

EricSUN(RailsBeijing)

unread,
Oct 8, 2007, 9:13:44 PM10/8/07
to Ruby on Rails: Talk

sergi...@gmail.com

unread,
Oct 9, 2007, 4:04:31 AM10/9/07
to Ruby on Rails: Talk
I've read this and I understand the RESTful concept. Though it's not
obvious that I shouldn't make the categories resources.

Reply all
Reply to author
Forward
0 new messages