I had been in troubles setting up some relationships… in Rails too. Just to avoid the same headhecks in the future I decided to write this post. This code is supposed to help me (and you!) to make things easier next time.
The code has been tested on ruby-2.3.0 and Rails 5.0.2.
One_to_many relationship
As an example, one Author can have many Interests.
Using has_and_belongs_to_many
Using has_many through
I prefer to use the ‘has_many :through’ based solution. It is possible to create a Model associated to the relationship, with attributes, so the solution is much more flexible.