Forum: Ruby on Rails Triple relationship

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.
15b697654c3e118f6f06fd761980d268?d=identicon&s=25 vanesam (Guest)
on 2006-04-01 11:29
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
2f62bd976630967049a829b6e96d07c4?d=identicon&s=25 unknown (Guest)
on 2006-04-01 12:29
(Received via mailing list)
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 <vanesam@ece.ubc.ca>
To: rails@lists.rubyonrails.org
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
675475d0b65710be6d992eb5eb2c61c2?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-04-01 18:21
(Received via mailing list)
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
15b697654c3e118f6f06fd761980d268?d=identicon&s=25 Vanessa Abar (vanesam)
on 2006-04-01 21:41
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 <vanesam@ece.ubc.ca>
> To: rails@lists.rubyonrails.org
> 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
E962ae295d82a05193cc180c12bda5d8?d=identicon&s=25 Juan =?ISO-8859-1?Q?Jos=E9?= Vidal (Guest)
on 2006-04-02 02:35
(Received via mailing list)
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/

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
69bc2439b61e69b372caae8f3affe653?d=identicon&s=25 Bryan Buecking (Guest)
on 2006-04-04 07:05
(Received via mailing list)
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
This topic is locked and can not be replied to.