First, thank you for your reply and for your explanation of what UID
means. In my app I have the following table for private messages:
create_table "private_messages", force: true do |t|
However, when I enter, PrivateMessages, in rails console, I get a syntax
error: syntax error, unexpected tSTRING_BEG, expecting =>
belongs_to :sender, foreign_key: 'sender_uid', :class_name 'User'
The same error appears for receiver_uid...
Per your explanation, does this mean that I have to add a columns,
sender and receiver, to my users table?
No, remember, whether the message is sent or received by a person is important only to the message, not the person. So the Users model should not know or care about messages.
Or, should I make a
sent_messages table and a received_messages table, giving them a foreign
I think that your PrivateMessage model would care about to and from, so it would be the place to put the foreign keys -- sender_uid and receiver_uid or whatever you choose to call them. In the context of a single message object, who it is to and who it is from are important, and more critically, are references to existing User objects.
Or in my User model, should I create the following associations:
has_many :sent_messages, class_name: 'PrivateMessages',
has_many :received_messages, class_name: 'PrivateMessages',
This would be the proper inverse of the previous suggestion, but remember, always set the Model as a singular. The database table name is plural, as is the Controller. Pluralization is confusing at first, but when you think about objects as objects, it makes perfect sense to name things that way.
Actually, reading through this twice before sending, I think the best thing for you to do is read this page:
...paying particular attention to this: http://guides.rubyonrails.org/association_basics.html#bi-directional-associations
And I would keep on doing what you're doing -- stub something out quickly in scaffold and test it in console. If it's too hard, you're doing it wrong! (That's always been my experience.)