I am trying to connect two classes (conversation and user) by a many-to-many relationship in Ruby on Rails. I set them both up and added a connection table called conversations_custom_users to connect them and it was working. Once we needed our User model to inherit from another User model, setting conversations in a user object was failing and looking for a connection table with the parent class.
My classes and the conversation migration looks like below (I haven’t modified the User migration for the many-to-many relationship):
class CustomUser < Spree::User serialize :resources, Array has_and_belongs_to_many :conversations, :foreign_key => :conversation_ids, class_name: 'Conversation' end class Conversation < ApplicationRecord has_and_belongs_to_many :receiver, :foreign_key => :receiver_id, class_name: 'CustomUser' end class CreateConversations < ActiveRecord::Migration[6.1] def change create_table :conversations do |t| t.timestamps end create_table :conversations_custom_users, id: false do |t| t.belongs_to :conversation, foreign_key: 'conversation_id', index: true t.belongs_to :custom_user, foreign_key: 'receiver_id', index: true end end end
I think I shouldn’t need to add another table called conversations_spree_users, but I also tried adding one. It didn’t solve the problem since then Rails was looking for a spree_user_id field. I also tried adding the spree_user_id field to the conversations_spree_users table, but it wouldn’t migrate because it was a duplicate column name!
I think I’m missing something about many-to-many relations or inheritance or both in Ruby. If someone can help with this issue I’d really appreciate it.