Model Question

Hi --

Hello,

so I have a situation that I'm finding hard to model. I have two objects, User and Message. Here's a condensed version of what they look like below:

User: ----- id: integer name: string

Message: -------- id: integer from_id: integer (should point to a user) to_id: integer (should point to a user) body: string

I'm working on my tests and have my fixture data all in place. My first test goes something like this:

m = Message.find(1) assert m.to_user.id==2 assert m.from_user.id==1

db table looks like (hope the 'formatting' works):

Messages ------------------------------- > id | to_id | from_id | body | ------------------------------- > 1 | 2 | 1 | blah | > 2 | 1 | 2 | foo | -------------------------------

The user table is should be self-explanatory. I also have no foreign keys specifically set as I read in various blog postings that this was not necessary from a rails standpoint and I have other models & relationships working fine w/out it.

My big prob is I'm not able to figure out the right relationship type: messages belongs_to user and user has many message_tos? That kind of stuff. This is a fairly simple association and I dunno why I'm having so many probs w/it.

Any suggestions? Been going round with this thing for hours now. Tia!

If you're in the market for modifying the schema, you might want to do what's described here:

http://dablog.rubypal.com/articles/2006/06/25/modeling-many-to-many-i-guess-i-was-right-the-first-time

Basically the model I used involved the notion of a Correspondence. Every message belongs to a correspondence. Every message also belongs to a sender and a receiver. This might run aground on messages sent to many people (the whole thrust of the application I was working on was private exchanges), but it's another example of how one might think in terms of an intermediate class and the CRUD alignment that might bring about.

David