Reputation: 42513
I have a Model of Key Terms
that can belong to many Articles
(1 Term can be used in many articles)? so a user can add key terms used and their explanations to articles for which they are used in? what would be the best way to establish this relationship?
Thank You
Upvotes: 1
Views: 1671
Reputation: 37507
You'll want to use a many-to-many relationship so you'll need to use has_and_belongs_to_many
(see here). And you'll need to create an interim table for key_term_id
and article_id
columns named key_terms_articles
to maintain the connection.
Upvotes: 5
Reputation: 477
I think what you want is what's referred to as a HABTM (has and belongs to many) relationship. There's a lot of stuff out there on the topic, and it can be a bit confusing at first to grasp. Try searching for "activerecord HABTM" or "Activerecord polymorph" and that should get you started.
Upvotes: 1