Private Message System Rails 4

Hi All,

I am trying to build a private message system on rails 4. I found a great post here, Rails threaded private messaging - Stack Overflow, on stackover flow, but it is unclear to me what the tables should look like. Does anyone have any suggestions with regards to building out the tables?

Secondary question: Often when I Google this question, I see symbols or strings that look like this: sender_uid or "receiver_uid" -- what is _uid? I am familiar with, _id, when we are dropping a foreign key in a table. Is appending, _uid, just another syntax for creating foreign keys in tables? Does that mean there are, senders and receivers, tables?

Hi All,

I am trying to build a private message system on rails 4. I found a great post here, Rails threaded private messaging - Stack Overflow, on stackover flow, but it is unclear to me what the tables should look like. Does anyone have any suggestions with regards to building out the tables?

I strongly recommend that you use a free tutorial like the http://railstutorial.org by Michael Hartl to build a simple "starter" application first. Once you work all the way through that course you will have a strong and well-rounded idea of both how to answer this question, but how to solve others that will no doubt arise as you go.

Secondary question: Often when I Google this question, I see symbols or strings that look like this: sender_uid or "receiver_uid" -- what is _uid? I am familiar with, _id, when we are dropping a foreign key in a table. Is appending, _uid, just another syntax for creating foreign keys in tables? Does that mean there are, senders and receivers, tables?

UID is an acronym for Unique ID. I think in the context of your project, that this means there is a single "users" table, and each user may be, at one time or another, a sender, receiver, or possibly both. Whether they are a sender or receiver means something specific in how you would render the message on screen (whose name goes on top, whose name goes on the bottom) but doesn't change the specific "user-ness" of each person.

Walter

Hi Walter,

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|     t.integer "sender_uid"     t.integer "receiver_uid"     t.text "subject"     t.text "body"     t.datetime "created_at"     t.datetime "updated_at"   end

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? Or, should I make a sent_messages table and a received_messages table, giving them a foreign key, user_id?

Or in my User model, should I create the following associations:         has_many :sent_messages, class_name: 'PrivateMessages', foreign_key: 'sender_uid'    has_many :received_messages, class_name: 'PrivateMessages', foreign_key: 'receiver_uid'

I really appreciate your feedback, Walter.

Sorry, just an edit for the code in the User model: belongs_to :sender, foreign_key: 'sender_uid', class_name: 'User'   belongs_to :receiver, foreign_key: 'receiver_uid', class_name: 'User'

Robert Jewell wrote in post #1132668:

Or in my User model, should I create the following associations:         has_many :sent_messages, class_name: 'PrivateMessages', foreign_key: 'sender_uid'    has_many :received_messages, class_name: 'PrivateMessages', foreign_key: 'receiver_uid'

I really appreciate your feedback, Walter.

And now there's no more syntax Error

Robert Jewell wrote in post #1132668:

Hi Walter,

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|    t.integer "sender_uid"    t.integer "receiver_uid"    t.text "subject"    t.text "body"    t.datetime "created_at"    t.datetime "updated_at" end

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 key, user_id?

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', foreign_key: 'sender_uid'   has_many :received_messages, class_name: 'PrivateMessages', foreign_key: 'receiver_uid'

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: Active Record Associations — Ruby on Rails Guides

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.)

Walter

Hey Walter,

Your advice is really solid, and I appreciate it a lot. Thank you for pointing out the note about pluralization. I fixed it accordingly. However, I not follow you so well here:

This would be the proper inverse of the previous suggestion... Actually, reading through this twice before sending, I think the best thing for you to do is read this page:

Active Record Associations — Ruby on Rails Guides

...paying particular attention to this:

Moreover, I am not too sure if what I have done has further complicated things :slight_smile:

I am trying to create a notifications center for my users to see their project requests and private messages -- as well as any other related activity in our community. There is a lot to see here with regards to my application's file structure, and I would like to show you the repo. However, its private, and I do not know how to let someone just view it.

I created a gist for you to see what I've done for the following tables and models: PrivateMessage, User and Notification: https://gist.github.com/rjewell2200/8364291

Also, here are the relevant routes: https://gist.github.com/rjewell2200/8364263

and the routes.rb file: https://gist.github.com/rjewell2200/8364354

I've also created a model backed form to hit the database each time that a user goes to another user's profile to send the recipient a message. While each POST is adding rows to the notifications table, all of the attributes of the notifications table are showing up as, nil. Here's the gist for file containing the form: https://gist.github.com/rjewell2200/8364382

Basically I think I've wired up everything correctly but every time I go to another users profile to send him/her a message, I can not view those notifications on the UI/UX and yet the model backed form is hitting the notifications database, although the notifications table's rows are not showing any of the rows attributes.

Can you guess why my polymorphic, notifications table is not receiving the attributes from the submitted private messages? Also, each POST fails to hit the private_messages tables as well.

Hey Walter,

Your advice is really solid, and I appreciate it a lot. Thank you for pointing out the note about pluralization. I fixed it accordingly. However, I not follow you so well here:

This would be the proper inverse of the previous suggestion... Actually, reading through this twice before sending, I think the best thing for you to do is read this page:

Active Record Associations — Ruby on Rails Guides

...paying particular attention to this:

Active Record Associations — Ruby on Rails Guides

Moreover, I am not too sure if what I have done has further complicated things :slight_smile:

I am trying to create a notifications center for my users to see their project requests and private messages -- as well as any other related activity in our community. There is a lot to see here with regards to my application's file structure, and I would like to show you the repo. However, its private, and I do not know how to let someone just view it.

I created a gist for you to see what I've done for the following tables and models: PrivateMessage, User and Notification: https://gist.github.com/rjewell2200/8364291

One thing leaps out at me here: I don't think you really want to make the first and last name unique -- anyone named Smith in your system? Any others?

Also, here are the relevant routes: https://gist.github.com/rjewell2200/8364263

and the routes.rb file: https://gist.github.com/rjewell2200/8364354

I've also created a model backed form to hit the database each time that a user goes to another user's profile to send the recipient a message. While each POST is adding rows to the notifications table, all of the attributes of the notifications table are showing up as, nil. Here's the gist for file containing the form: https://gist.github.com/rjewell2200/8364382

It might be instructional for you to post the controller that handles this form, I don't see anything wrong here, except unless you are adding the sender_uid in that controller, I don't see it being added to the params in the form anywhere.

Basically I think I've wired up everything correctly but every time I go to another users profile to send him/her a message, I can not view those notifications on the UI/UX and yet the model backed form is hitting the notifications database, although the notifications table's rows are not showing any of the rows attributes.

Can you guess why my polymorphic, notifications table is not receiving the attributes from the submitted private messages? Also, each POST fails to hit the private_messages tables as well.

I think you may be missing something in the controller then, that's the only thing I haven't seen yet, and everything so far has seemed reasonable.

Walter

Walter Davis wrote in post #1132809:

It might be instructional for you to post the controller that handles this form, I don't see anything wrong here, except unless you are adding the sender_uid in that controller, I don't see it being added to the params in the form anywhere.

...

I think you may be missing something in the controller then, that's the only thing I haven't seen yet, and everything so far has seemed reasonable.

Walter

Hi Walter, per your request, here's a gist of my users_controller, which corresponds with the form's view: https://gist.github.com/rjewell2200/8364397

I'll read the literature that you suggest for the associations. I do not know why the form is not hitting the database correctly for the private_messages and notifications tables.

I've also made the repo public if you wanted to have a deeper look: https://github.com/rjewell2200/gr-comm

Robert Jewell wrote in post #1132839:

Hi Walter, per your request, here's a gist of my users_controller, which corresponds with the form's view: https://gist.github.com/rjewell2200/8364397

It looks like I found the problem. I needed to add the following code under the, send_private_message method, in the users controller:   if @private_message.save     flash[:notice] = "Message Sent."     redirect_to root_path   else     render :new   end @notification = Notification.create(notificationable_type: @private_message, user: current_user, notification: params[:notification]) end

However, now a new problem has arrived. Numerically ordered numbers are filling the cells within the polymorphic column, t.string "notificationable_type" -- I do not know why this is happening and not the name of the type of object.