Umar Ghouse
Umar Ghouse

Reputation: 458

What's the best practice for connecting a table to multiple other tables where each has a many-to-many relationship?

I have a rails blog, with a PostgreSQL database, where I have a Post model and PodcastNote model. I want to add a 'Tag' model, where tags can be for both posts and podcast_notes. In both cases, it will be a many-to-many relationship. My question is what is the best practice for handling that?

I can think of 2 ways:

  1. Have 1 join model that connects tags to both posts and podcast_notes.

  2. Have 2 join models - one that connects tags to posts and another that connects tags to podcast_notes

Which would be considered the best practice in this case?

If best practice is option 1 above, how would I go about doing that?

Upvotes: 0

Views: 47

Answers (1)

eikes
eikes

Reputation: 5071

One join model with a polymorphic association is the way most tagging gems solve this.

https://github.com/mbleigh/acts-as-taggable-on

https://github.com/pat/gutentag

Upvotes: 3

Related Questions