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!
Anyway, I tried to apply his principles to your model and here’s my
suggestion. You may come up with an even better one after further
reflection.
How about only including the sender’s user_id in the messages table and
adding a new table named something like MsgAddressees? A message may be
addressed to more than one person, at least in most situations I know
of. So you would have
Message:
id: integer
user_id: integer (should point to the user who sent the message)
body: string
MsgAddressee:
id: integer
message_id: integer (should point to a message)
user_id: integer (should point to a user who the message was sent to)
Then you can have a User has_many Messages and a Message belongs_to a
User but has_many MsgAddresses.
Your situation may be more complex but I hope this helps you through the
logjam. I highly recommend David’s talk!
Yah, I watched DH’s keynote - always inspiring. However, I forgot some
of the things you mentioned.
And the more I look at it, the more I like yr take on it. I suppose I
was looking at it very one-sided and really, I put this out there for
another point of view. I think your approach works better and satisfies
the thing that stuck in my head most which was how to get to a ‘to’ msg
out the quickest. But I suppose, I can query the addresse table just
fine in those cases and work my way up the graph and back. So this is
good.
Anyway, thx again! I’m gonna go back and re-visit the talk tonight.
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
–
Q. What’s a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (Ruby for Rails)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)