I have two models:
class RecordProduct < ApplicationRecord
end
class ShopiTagProduct < ApplicationRecord
end
And I want to create a many to many relationship between models. I execute this command:
rails g migration CreateJoinTableRecordProductsShopyTagProducts record_products:references:index shopi_tag_products:references:index
This command create this migration:
class CreateJoinTableRecordProductsShopyTagProducts < ActiveRecord::Migration[5.2]
def change
create_join_table :record_products, :shopi_tag_products do |t|
t.references :record_products, foreign_key: true, index: {name: :productId}
t.references :shopi_tag_products, foreign_key: true, index: {name: :tagId}
end
end
end
when execute rake db:migrate throws an error: 'name_index_tooooo_long' on table 'name_new_table' is too long; the limit is 64 characters.
So, I change the migration class to:
class CreateJoinTableRecordProductsShopyTagProducts < ActiveRecord::Migration[5.2]
def change
create_join_table :record_products, :shopi_tag_products do |t|
t.references :record_products, foreign_key: true, index: {name: :productId}
t.references :shopi_tag_products, foreign_key: true, index: {name: :tagId}
end
end
end
When I execute rake db:migrate it create the table, but with columns are repetead:
record_product_id
shopi_tag_product_id
record_products_id
shopi_tag_products_id
I have two question.
First: Whay the columns are repetead? What is the correct way for columns to be generated properly?
Second: Should I manually add the relations (has_and_belongs_to_many) in the models or are these added automatically?