Mo.
Mo.

Reputation: 42513

RoR: belongs_to_many relationship in Rails?

I have a Model of Key Termsthat 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

Answers (2)

John Beynon
John Beynon

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

John
John

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

Related Questions