A
A
Anton2016-01-23 14:37:15
Ruby on Rails
Anton, 2016-01-23 14:37:15

What is the best way to implement 1:N communication in a RoR project?

Hello!
One news can be in several categories (have several IDs of different categories).
To do this, you need to use the third table - categories_for_news - with the fields: id, news_id, category_id.
At this stage, everything is clear. The question is this: "What is the best way to organize this in a RoR project?"
I mean, is it necessary to create a model for this case. In general, I am new to RoR, and I would like to ask you for a piece of code with this implementation as an example, as well as accompanying comments.
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy Trebin, 2016-01-23
@hummingbird

You need to create 2 models Categories and News and organize a many-to-many relationship. How to do this is written in the documentation with examples: Has_many
:through
relationship Has_and_belongs_to_many relationship
Choosing between has_many :through and has_and_belongs_to_many

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question