What join table in this case is called in Rails 3.1?

I have two tables with the has_and_belongs_to_many relation: categories and raw_categories

Should the table be called categories_raw_categories ?

+4
source share
1 answer

Yes, the join table is named after combining the two tables listed in alphabetical order. Since categories higher in the alphabet than raw_categories , the join table is called categories_raw_categories . Please note: if you are migrating, you need to create a separate migration for this connection table.

For more information on HABTM relationships and required connection tables, see here: http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_and_belongs_to_many

Also note that you can set your own name for the connection table if you wish. Example (if you want to call the category_associations connection table):

 # Category model has_and_belongs_to_many :raw_categories, :join_table => 'category_associations' # RawCategory model has_and_belongs_to_many :categories, :join_table => 'category_associations' 

You can also always explicitly make a join table a first-class model using has_many :though for has_many :though models. Following the example above, you can make CategoryAssociation a real model and attach it to the other two as follows:

 # CateogoryAssociation model belongs_to :category belongs_to :raw_category # Category model has_many :category_associations has_many :raw_categories, :through => :category_associations # RawCategory model has_many :category_associations has_many :categories, :through => :category_associations 
+4
source

Source: https://habr.com/ru/post/1383393/


All Articles