I'm implementing the has_many :through association in rails.
I want to discuss the best practies to naming the join table. I have found this link: http://guides.rubyonrails.org/association_basics.html#tips-tricks-and-warnings but it descript the naming convention for has_and_belongs_to_many
what is the best join table naming convention for the has_many :through association?
Caveat to my answer: I am not a rails expert. This is my opinion from recent experience.
For the has_and_belongs_to_many relation the naming convention is that the the model for the relationship should include the tables you wish to relate in alphabetical order. So for joining pupils to groups you would use group_pupils, so that rails can find the table from the information in the pupils and groups model.
For a has_many :through relation you build a model for the join table. So for the example with pupils and groups you might call rails g model PupilGroup pupil:references group:references to build your model and migrations. Alternatively, you can use a meaningful name such as pupil_groupings or maybe just groupings or sets. It seems that as the model contains the information for setting up the join, in the example belongs_to :pupil and belongs_to :group the restriction on naming the table is lifted, as rails has another way of knowing which table is used to store the relation.
Therefore you should choose a sensible name that supports clarity and maintenance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With