# Complex Association - Please help

**URL:** <https://www.ruby-forum.com/t/complex-association-please-help/169564>\
**Category:** Rails\
**Created:** [June 5, 2009, 10:43am UTC](https://www.ruby-forum.com/t/complex-association-please-help/169564 "2009-06-05T10:43:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hirak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7feea3/32.png) [@Hirak](https://www.ruby-forum.com/u/Hirak)\
**Post date:** [June 5, 2009, 10:43am UTC](https://www.ruby-forum.com/t/complex-association-please-help/169564/1 "2009-06-05T10:43:23Z")

</div>

Hi All,  
I am a newbie in rails. I am creating a new rails  
application. Here I have 4 models.

1. User
2. Item
3. Application
4. Tag

The concept is

- Item belongs to User
- Item has many Tags
- Item has many application through tags
- Tags are hierarchical (i.e a tree structure, a child tag can be  
related to a parent tag.)

Please help me, how to handle these Associations.

Thanks  
Hirak

---

<div class="post-metadata">

**Author:** ![Hirak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7feea3/32.png) [@Hirak](https://www.ruby-forum.com/u/Hirak)\
**Post date:** [June 5, 2009, 10:57am UTC](https://www.ruby-forum.com/t/complex-association-please-help/169564/2 "2009-06-05T10:57:34Z")

</div>

comments inline

On Fri, Jun 5, 2009 at 1:57 PM, Hirak [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi All,  
> I am a newbie in rails. I am creating a new rails  
> application. Here I have 4 models.
> 
> 1. User
> 2. Item
> 3. Application
> 4. Tag

ok

> The concept is
> 
> - Item belongs to User
> - Item has many Tags

It means you have tags table will have item\_id

- Item has many applications through tags

for above you must have some relations between application and tags  
table.

- Tags are hierarchical (i.e a tree structure, a child tag can be

> related to a parent tag.)

tag belongs\_to parent\_tag :class\_name =\> “Tag”, :foreign\_key =\>  
“parent\_id”

You must have parent\_id field in tags table.  
If parent\_tag is nil then you can identify tag as child\_tag

> Please help me, how to handle these Associations.
> 
> Thanks  
> Hirak

hope this will help !

-Sandip R~

> >

–  
Ruby on Rails Developer

> **[Blogs - Neuigkeiten - Artikel - Webseiten - Medien](https://www.sosblog.com/)**
>
> Auf SOSblog geht es um aktuelle Blogs, Neuigkeiten, Artikel, Webseiten und Medien. Wir bringen interessante neue Webseiten aus den Tiefen des Webs.

> **[Judul Situs](https://funonrails.wordpress.com/)**
>
> “Cinta tidak mengenal batas. Cinta mampu melewati rintangan, melompati pagar, menembus dinding untuk mencapai tujuannya dengan penuh harapan.” — Maya Angelou

> **[Josh Software | Custom Software Development Services & Solutions](https://joshsoftware.com/)**
>
> JoshSoftware offers custom software development Services & Solution that provides enterprise, software development services for web & mobile.

---

<div class="post-metadata">

**Author:** ![Hirak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/7feea3/32.png) [@Hirak](https://www.ruby-forum.com/u/Hirak)\
**Post date:** [June 5, 2009, 11:08am UTC](https://www.ruby-forum.com/t/complex-association-please-help/169564/3 "2009-06-05T11:08:11Z")

</div>

On Jun 5, 1:56 pm, Sandip R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 1. Application
> 
> tag belongs\_to parent\_tag :class\_name =\> “Tag”, :foreign\_key =\> “parent\_id”
> 
> hope this will help !
> 
> -Sandip R~
> 
> –  
> Ruby on Rails Developerhttp://sandip.sosblog.comhttp://funonrails.wordpress.comwww.joshsoftware.com

Thanks Sandip you got it almost right, but what I meant is

Items table will have tag\_id

How that association needs to be handled ?

Thanks again for your reply.

– Hirak
