I have a join table, which I didn't name using traditional Rails conventions. It joins Participant
and Group
models (associated using has_and_belongs_to
) and would normally be named groups_participants
.
I have tried to destroy a record in the Group
model, and received an invalid statement error, "Could not find table 'groups_participants
'"
My join table is not named groups_participants
, but rather named "matchups
"
Is this the problem? Do I need to change my join table name? From reading several articles, having an alias for your join table is acceptable.
This is my join table from schema.rb
create_table "matchups", id: false, force: :cascade do |t|
t.integer "group_id", null: false
t.integer "participant_id", null: false
t.index ["group_id", "participant_id"], name: "index_matchups_on_group_id_and_participant_id"
t.index ["participant_id", "group_id"], name: "index_matchups_on_participant_id_and_group_id"
end
This is the code highlighted in my error page:
def table_structure(table_name)
structure = exec_query("PRAGMA table_info(#{quote_table_name(table_name)})", 'SCHEMA')
raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if structure.empty?
table_structure_with_collation(table_name, structure)
end
Trying to delete a record existing in my Group
model, prevented from doing so with unable to find 'groups_participants
' as a reason.
Since your join table doesn't follow the Rails naming convention, make sure to tell Rails what the correct name is by specifying the join_table
option on the has_and_belongs_to_many
association:
has_and_belongs_to_many :participants, join_table: :matchups