# About nested resources

**URL:** <https://www.ruby-forum.com/t/about-nested-resources/160943>\
**Category:** Rails\
**Created:** [February 21, 2009, 6:53am UTC](https://www.ruby-forum.com/t/about-nested-resources/160943 "2009-02-21T06:53:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [February 21, 2009, 6:53am UTC](https://www.ruby-forum.com/t/about-nested-resources/160943/1 "2009-02-21T06:53:57Z")

</div>

Hi all,

I have several resources:

map.resources :articles, :has\_many =\> [:comments]  
map.resources :movies, :has\_many =\> [:comments]  
map.resources :photos, :has\_many =\> [:comments]

all of them have a nested resource named comments, new comments are  
able to be created by POST to URLs like article\_comments\_url. My problem  
is, when I’m in the comments\_controller, how can I know which resource  
this comment belongs to? By reading the params? I think this is not  
decent, are there any better ways? Thanks in advance!

Cheers,  
Difei

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [February 21, 2009, 2:29pm UTC](https://www.ruby-forum.com/t/about-nested-resources/160943/2 "2009-02-21T14:29:50Z")

</div>

Difei,

If a comment only belongs to either an article, movie or a photo  
(never more than one) you could add methods in the model to determine  
which it is for.

For example:

class Comment

def article\_comment?  
!article\_id.nil?  
end  
end

@comment.article\_comment? will return true if this is a comment for an  
article and false if not.

You could render your view accordingly for each type of comment.

Hope that helps?

On Feb 21, 5:53 am, Difei Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [February 22, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/about-nested-resources/160943/3 "2009-02-22T16:27:52Z")

</div>

Gavin wrote:

> Difei,
> 
> If a comment only belongs to either an article, movie or a photo  
> (never more than one) you could add methods in the model to determine  
> which it is for.
> 
> For example:
> 
> class Comment
> 
> def article\_comment?  
> !article\_id.nil?  
> end  
> end
> 
> @comment.article\_comment? will return true if this is a comment for an  
> article and false if not.
> 
> You could render your view accordingly for each type of comment.
> 
> Hope that helps?

Hi Garvin, but your way is to decide the father of an existed comment.  
if I just received POST to /articles/:article\_id/comments/:comment\_id,  
and I have a:  
@comment = Comment.new(params[:comment]), how could I tell this @comment  
is for article but not photo? By test params?

Thanks.

> On Feb 21, 5:53ï¿½am, Difei Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [February 22, 2009, 11:26pm UTC](https://www.ruby-forum.com/t/about-nested-resources/160943/4 "2009-02-22T23:26:56Z")

</div>

That seems like a fine solution.

what about just creating three different types of comments,  
article\_comment, movie\_comment, photo\_comment?

I’m not necessarily advocating for this, but it’s an option, and it  
may be better. That way, you don’t need the functions at all. and it  
should help keep your controllers & views simple.

There is in some sense a problem of code duplication between the three  
comment resources, but you may be able to DRY this up with helper  
methods.

-Gabe

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [February 22, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/about-nested-resources/160943/5 "2009-02-22T18:12:15Z")

</div>

Ah - I think I understand now.

I would add a column to the comments table called ‘kind’ (as in, ‘kind  
of comment’)

The column should be an integer column with limit 1.

Then assign a different integer value to determine what kind of  
comment it is.

i.e.,  
1 = article  
2 = photo  
3 = movie

When you creating a new comment, simply check the value of this  
integer and redirect accordingly.

Does that help?

On Feb 22, 3:27 pm, Difei Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [February 22, 2009, 11:32pm UTC](https://www.ruby-forum.com/t/about-nested-resources/160943/6 "2009-02-22T23:32:48Z")

</div>

Hypothetically, if one were to create three different types of comment  
would you just create a Comment model and then three other models that  
were subclasses?

I’ve never played with subclasses in a rails app (other than the ones  
specified by rails) so I’m curious as to which whether this would be  
the way to do it?

Gav

---

<div class="post-metadata">

**Author:** ![difei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a4c791/32.png) [@difei](https://www.ruby-forum.com/u/difei)\
**Post date:** [February 23, 2009, 5:35am UTC](https://www.ruby-forum.com/t/about-nested-resources/160943/7 "2009-02-23T05:35:12Z")

</div>

Hi all,

Since my commentable resources are not too many, I decided to do  
something like this in the comment controller:

before\_filter: find\_resource

private  
def find\_resource  
if params[:article\_id]  
@resource = Article.find(params[:article\_id])  
elsif params[:photo\_id]  
@resource = Photo.find(params[:article\_id])  
end  
end

Thank y’all,  
Difei
