This is probably a newbie question, but I cannot figure out the best solution.
I have a Color model and a Cover model.
Most of the time, a cover has_one :color. Maybe one out of one hundred cases, the cover has_many :colors.
Do I have to implement a has_many :through association between the two models? It seems redundant. What’s the best practice to define such a relationship?
This is probably a newbie question, but I cannot figure out the best solution.
Share
I think you should cover the general case, when
Cover has_many :colors. So this is a many-to-many relationship. In this case, you will have 2 options:Use join table, if you just need to store
cover_idandcolor_idin the join table, so you will usehas_and_belongs_to_manyto define your association. With this option, You will have to create join table.Use join model, if you need to store not only
cover_idandcolor_id, but also other attributes, and you need to do some calculation, so you have to usehas_many :throughto define your association. With this option, You will have to create new model.