Create tables:
bin/rails g model Location name:string
bin/rails g model User name:string
bin/rails g model Checkin
Run migration:
bin/rails db:migrate
Create migration:
bin/rails g migration ConnectLocationsAndUsers
Add to migration file:
class ConnectLocationsAndUsers < ActiveRecord::Migration
def change
add_column :checkins, :location_id, :integer
add_column :checkins, :user_id, :integer
add_index :checkins, [:location_id, :user_id] #, name: 'index__checkin__location__user' # if index name is too long
end
end
Run migration:
bin/rails db:migrate
Add to models/location.rb
class Location < ActiveRecord::Base
has_many :checkins, dependent: :destroy
has_many :users, through: :checkins
end
Add to models/user.rb
class User < ActiveRecord::Base
has_many :checkins, dependent: :destroy
has_many :locations, through: :checkins
end
Add to models/checkins.rb
class Checkin < ActiveRecord::Base
belongs_to :location
belongs_to :user
end
User.update(location_ids:[])