Composite index - M: N in Ruby on Rails

0

I need to add a double index to the N to N ratio of the courses_internships table. it would look something like this: add_index[:course_id, :internship_id] .

However, when I created the link table, the index was separated by the columns, not the index being both. How can I reverse this situation?

And ah, I use the has_and_belongs_to_many method

    
asked by anonymous 20.05.2015 / 18:14

1 answer

1

Your new index will be something like: (If you do not need it to be unique, just remove the unique: true from the line).

add_index :courses_internships, [:course_id, :internship_id], unique: true

Already to remove and add the indexes, you can use the following command:

$ rails g migration remove_separated_indexes_and_add_new_to_course_internships

and then edit the migration class with something like:

class RemoveSeparatedIndexesAndAddNewToCourseInternships < ActiveRecord::Migration
  def change
    remove_index :courses, :course_id
    remove_index :internships, :internship_id
    add_index :courses_internships, [:course_id, :internship_id], unique: true
  end
end

Thanks!

    
22.05.2015 / 23:17