Model relationship confusion

Hi all,

I’m thinking this must be pretty straightforward, but I’m finding
myself kind of going in circles in trying to model a simple “private
messaging” app.

I have two models: user and message

User
has many messages

Message
has a sender (User)
has many recipients (Users)

I’m at a bit of a loss as to how best define the relationships that
occur in a message. I want two relationships to the User model. One
for the sender and another for the recipients.

I’ve looked at STI but that doesn’t seem to be the correct solution
since a given User can be both a sender and recipient simultaneously.

Polymorphism seems to be the way to go, but I’m not clear on what that
would look like in this case.

I’m feeling that I’m missing something really really obvious here.

Rick

Rick,

I would look at using a User, Message, and Recepients model. The
recepients table could be a simple join table holding the message_id
and recepient_id. Then use has_many :through to access a message’s
recepients (User model).

Zack

On Tue, 2006-25-07 at 13:36 -0700, Zack C. wrote:

Rick,

I would look at using a User, Message, and Recepients model. The
recepients table could be a simple join table holding the message_id
and recepient_id. Then use has_many :through to access a message’s
recepients (User model).

Thanks Zack! That makes sense. Me thinks I was making it more
complicated than it needed to be. :slight_smile:

I should be able to handle the Sender of the message in the same way.
ie. create a Sender model which is a join between messages and users as
well.

Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]lid
http://lists.rubyonrails.org/mailman/listinfo/rails

Rick T. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs