views:

34

answers:

2

I'm coding a site that's using tags. Two other classes (Foo and Bar) both will use tags, but the tags used in Foo won't apply to Bar, and vice-versa. So I created the Tags table with both foo_id and bar_id columns, so when a record is saved, the foreign key for the correct object-type can be added.

Will this cause problems? Is there a more elegant way to store Tags?

+1  A: 

You are implementing a many-to-many relation.

Use a join table with tag_id, foo_id. Then in your model classes use has_and_belongs_to_many or has_many :through.

Use a separate join table for bar.

See http://blog.hasmanythrough.com/2006/04/20/many-to-many-dance-off

jakber
I've had some trouble with identifying many-to-many relationships. Thanks for the link!
b. e. hollenbeck
+1  A: 

You can either have separate join tables as jakber says, or use Single Table Inheritance, storing both foo tags and bar tags in the same table with a type field that specifies whether the id is for a foo or for a bar.

You might also like the acts _ as _ taggable _ on _ steroids plugin, it's very simple and handles tags nicely.

nfm