I have a User and MeetOption table. This is a many-to-many relationship, and I was able to create a join table using the create_join_table command:
rails g migration CreateJoinTableUsersMeetOptions users meet_options
This generated a migration file:
class CreateJoinTableUsersMeetOptions < ActiveRecord::Migration[5.0]
def change
create_join_table :users, :meet_options do |t|
# t.index [:user_id, :meet_option_id]
# t.index [:meet_option_id, :user_id]
end
end
end
I also created the association between user and meet_option models using has_and_belongs_to_many
class User < ActiveRecord::Base
has_and_belongs_to_many :meet_options
#More codes below
end
class MeetOption < ApplicationRecord
has_and_belongs_to_many :users
end
The association works fine, and I can query for example, user.meet_options in Rails console.
My question is: there is no model for the joined MeetOptionsUsers table, so how can I add records to it? Right now, I have to manually add rows, i.e. user_id = 1; meet_option_id = 2, using a GUI database software (Postico).
Is there a way to add records such as MeetOptionUser.create(user_id: 1, meet_option_id: 2) in Rails console, just like when there's an ActiveRecord model associated with it?