My current association looks like this:
class Location < ApplicationRecord
has_many :instructors
end
class Instructor < AppliactionRecord
belongs_to :location
end
I want to introduce a new location_instructors table that joins the above tables. Ideally, my association would turn into this:
class Location < ApplicationRecord
has_many :location_instructors
has_many :instructors, through: :location_instructors
end
class LocationInstructor < ApplicationRecord
belongs_to :location
belongs_to :instructor
end
class Instructor < ApplicationRecord
has_many :location_instructors
has_many :locations, through: :location_instructors
end
My main concern is that dropping the location_id column on the instructors table will make Rails not know which instructor belongs to which location anymore. How would I approach this new migration to make sure that there are location_instructors records for existing locations and instructors?