Hey guys I'm doing a has_many :through association in ruby on rails. And I want some help if it's ok with my example. So I pretend to do categories for Posts. Supposing I already have builded the class post. So for create the database for categories I pretend to do like these:
class CreateCategories < ActiveRecord::Migration[5.0]
def change
create_table :categories do |t|
t.string :name
t.timestamps
end
end
def change
create_table :categorizations do |t|
t.belongs_to :post, index: true
t.belongs_to :category, index: true
t.integer :position
t.timestamps
end
add_index :categorizations, [:product_id, :category_id], unique: true
end
end
It's ok these index's for boost the database?
Yes, that multi-column index that you've defined at the bottom:
add_index :categorizations, [:product_id, :category_id], unique: true
is valid. But it's also unnecessary and likely less performant than the single column indexes that you have individually defined on product_id
and category_id
.