Check out example codes for "rails has_many_and_belongs_to PG::UndefinedTable: Error: relation does not exist". It will help you in understanding the concepts better.

Code Example 1

# Migration file should use create_join_table() rather than create_table
# example:
create_join_table :assemblies :parts do |t|
  t.index %i[part_id assembly_id]
  t.index %i[assembly_id part_id]
end

# sources: https://apidock.com/rails/v6.0.0/ActiveRecord/ConnectionAdapters/SchemaStatements/create_join_table
# https://stackoverflow.com/questions/28902916/pgundefinedtable-error-relation-categories-posts-does-not-exist

Learn ReactJs, React Native from akashmittal.com