Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails best practies naming conventions join table has_many through [closed]

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?

like image 934
user1066183 Avatar asked Jan 20 '26 06:01

user1066183


1 Answers

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

like image 105
Russell Ormes Avatar answered Jan 23 '26 00:01

Russell Ormes