# Triple relationship

**URL:** <https://www.ruby-forum.com/t/triple-relationship/56584>\
**Category:** Rails\
**Created:** [April 1, 2006, 11:29am UTC](https://www.ruby-forum.com/t/triple-relationship/56584 "2006-04-01T11:29:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanesam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@vanesam](https://www.ruby-forum.com/u/vanesam)\
**Post date:** [April 1, 2006, 11:29am UTC](https://www.ruby-forum.com/t/triple-relationship/56584/1 "2006-04-01T11:29:53Z")

</div>

Hi list,

I need to represent a relationship between three tables:

Tags (id, name)  
Users (id, name, email, … )  
Documents (id, title, … )

I created a forth table called Assignments(id, tag-id, user-id,  
document-id, date).  
I have couple of questions:  
Should I use belongs-to and has-many to capture this? If so, How can I  
do that?  
should I have the id as the primary key in Assignment table or not?

Any help is most appreciated.

Thanks,  
-Vanesa

---

<div class="post-metadata">

**Author:** ![vanesam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@vanesam](https://www.ruby-forum.com/u/vanesam)\
**Post date:** [April 1, 2006, 12:29pm UTC](https://www.ruby-forum.com/t/triple-relationship/56584/2 "2006-04-01T12:29:49Z")

</div>

If you are attempting to implement tagging functionality on your  
documents then you should try to use acts\_as\_taggable gem or  
acts\_as\_taggable plugin. (I used gem in my use case, because it was more  
mature).

Chances are that you want to users to be able to tag documents? if thats  
the case then using acts\_as\_taggable gem will make your life easy. get  
back to me, if that is the case I can guide you further.

Anu

----- Original Message ----  
From: vanesam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: removed\_email\_address@domain.invalid  
Sent: Saturday, April 1, 2006 1:29:53 AM  
Subject: [Rails] Triple relationship

Hi list,

I need to represent a relationship between three tables:

Tags (id, name)  
Users (id, name, email, … )  
Documents (id, title, … )

I created a forth table called Assignments(id, tag-id, user-id,  
document-id, date).  
I have couple of questions:  
Should I use belongs-to and has-many to capture this? If so, How can I  
do that?  
should I have the id as the primary key in Assignment table or not?

Any help is most appreciated.

Thanks,  
-Vanesa

---

<div class="post-metadata">

**Author:** ![vanesam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@vanesam](https://www.ruby-forum.com/u/vanesam)\
**Post date:** [April 1, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/triple-relationship/56584/3 "2006-04-01T18:21:55Z")

</div>

On Sat, Apr 01, 2006 at 11:29:53AM +0200, vanesam wrote:  
} Hi list,  
}  
} I need to represent a relationship between three tables:  
}  
} Tags (id, name)  
} Users (id, name, email, … )  
} Documents (id, title, … )  
}  
} I created a forth table called Assignments(id, tag-id, user-id,  
} document-id, date).  
} I have couple of questions:  
} Should I use belongs-to and has-many to capture this? If so, How can I  
} do that?  
} should I have the id as the primary key in Assignment table or not?

Tags has\_many Assignments  
Users has\_many Assignments  
Documents has\_many Assignments  
Assignments belongs\_to Tag, User, Document

Someone mentioned acts\_as\_taggable, which only really makes sense if a  
single Assignment (or other model) has zero or more tags, rather than  
the  
single tag you seem to be indicating.

} Any help is most appreciated.  
} Thanks,  
} -Vanesa  
–Greg

---

<div class="post-metadata">

**Author:** ![vanesam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@vanesam](https://www.ruby-forum.com/u/vanesam)\
**Post date:** [April 1, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/triple-relationship/56584/4 "2006-04-01T21:41:14Z")

</div>

Hi,

I want the user to be able to tag the documents that exist in the  
system…also I like the system (the database) to keep track of “who  
tagged what with which tag and when”.  
I am new to both Ruby and Rails and don’t have that much experince any  
help is appreciated.

Thanks,  
-Vanesa

unknown wrote:

> If you are attempting to implement tagging functionality on your  
> documents then you should try to use acts\_as\_taggable gem or  
> acts\_as\_taggable plugin. (I used gem in my use case, because it was more  
> mature).
> 
> Chances are that you want to users to be able to tag documents? if thats  
> the case then using acts\_as\_taggable gem will make your life easy. get  
> back to me, if that is the case I can guide you further.
> 
> Anu
> 
> ----- Original Message ----  
> From: vanesam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> To: removed\_email\_address@domain.invalid  
> Sent: Saturday, April 1, 2006 1:29:53 AM  
> Subject: [Rails] Triple relationship
> 
> Hi list,
> 
> I need to represent a relationship between three tables:
> 
> Tags (id, name)  
> Users (id, name, email, … )  
> Documents (id, title, … )
> 
> I created a forth table called Assignments(id, tag-id, user-id,  
> document-id, date).  
> I have couple of questions:  
> Should I use belongs-to and has-many to capture this? If so, How can I  
> do that?  
> should I have the id as the primary key in Assignment table or not?
> 
> Any help is most appreciated.
> 
> Thanks,  
> -Vanesa

---

<div class="post-metadata">

**Author:** ![vanesam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@vanesam](https://www.ruby-forum.com/u/vanesam)\
**Post date:** [April 2, 2006, 2:35am UTC](https://www.ruby-forum.com/t/triple-relationship/56584/5 "2006-04-02T02:35:43Z")

</div>

El sÃ¡b, 01-04-2006 a las 21:41 +0200, vanesa mirzaee escribiÃ³:

> Hi,
> 
> I want the user to be able to tag the documents that exist in the  
> system…also I like the system (the database) to keep track of “who  
> tagged what with which tag and when”.  
> I am new to both Ruby and Rails and don’t have that much experince any  
> help is appreciated.

Hi Vanesa,

You can use UserStamp Plugin [http://www.delynnberry.com/pages/userstamp/](http://www.delynnberry.com/pages/userstamp/)

Copy&Paste from userstamp description:

“The Userstamp Plugin extends ActiveRecord::Base to add automatic  
updating of created\_by and updated\_by attributes of your models in much  
the same way that the ActiveRecord::Timestamp module updates  
created\_(at/on) and updated\_(at/on) attributes.”

Cheers!  
Juanjo

---

<div class="post-metadata">

**Author:** ![vanesam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7993a0/32.png) [@vanesam](https://www.ruby-forum.com/u/vanesam)\
**Post date:** [April 4, 2006, 7:05am UTC](https://www.ruby-forum.com/t/triple-relationship/56584/6 "2006-04-04T07:05:39Z")

</div>

vanesa mirzaee wrote:

> Hi,
> 
> I want the user to be able to tag the documents that exist in the  
> system…also I like the system (the database) to keep track of “who  
> tagged what with which tag and when”.  
> I am new to both Ruby and Rails and don’t have that much experince any  
> help is appreciated.

It seems to me you want a habtm relationship between documents and  
tags. In your joining table  
you should store doc\_id, tag\_id and user\_id. And as you are building  
your tags ensure to properly  
set the user\_id attribute (check the rails api for  
has\_and\_belongs\_to\_many to find out how this is done)

Then you can get all tags from your document by saying document.tags, or  
all documents that are tagged  
with something like by saying tag.documents and lastly, to get all  
user.tags just add a tags method in your users  
model like this:

def tags  
Tag.find\_by\_sql([“SELECT \* FROM tags, documents\_tags  
WHERE documents\_tags.user\_id = ? AND tags.id =  
documents\_tags.tag\_id”], self.id)  
end

–  
Bryan Buecking
