I ended up figuring it out via further trial and error. I was making it much harder than it was. Here are the correct changes for the User
and Message
classes and the relations between them:
Message
class Message < ActiveRecord::Base
belongs_to :user # This
belongs_to(
:conversation,
inverse_of: :messages
)
has_many(
:conversation_users,
through: :conversation
)
has_many( # This
:recipients,
class_name: "User",
through: :conversation_users,
source: :user
)
accepts_nested_attributes_for :conversation
User
class User < ActiveRecord::Base
has_many(
:conversation_users,
inverse_of: :user,
dependent: :destroy
)
has_many(
:conversations,
through: :conversation_users
)
has_many :messages # This
has_many( # This
:received_messages,
class_name: "Message",
through: :conversations,
source: :messages
)
The associations I marked with # This
are the four relations I was having trouble connecting in the first place. This messaging system is a unique four-model messaging system that supports (hypothetically) limitless conversation users. Its behavior is not unlike many other social network messaging systems out there. It is fully functional, and any of these four class objects can be accessed from any other related class object. I hope this Q/A will serve as a great reference for anyone trying to build a similar messaging system. Cheers!