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,
http://stackoverflow.com/questions/8405457/rails-threaded-private-messaging,
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?

On Jan 9, 2014, at 9:54 AM, Robert Jewell wrote:

Hi All,

I am trying to build a private message system on rails 4. I found a
great post here,
http://stackoverflow.com/questions/8405457/rails-threaded-private-messaging,
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 H. 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.


Allow me to add a little context here, seeing as that I did not in my
original post – I have built a basic application, and I have built
other applications (A pinterest app, a Reddit app, A black jack app) via
available tutorials etc…, although I have not built the Hartl’s
tutorial. Moreover, I have some experience with Ruby and OOP – granted,
not enough. I’m still fairly junior and would not try to sell myself
otherwise. Rails is just Ruby, and I am aware of that so I prefer
focusing on my Ruby competency, seeing as that makes rails development
more intelligent.

My application is a bootstrapping platform for nonprofits. One of its
main functionalities will allow nonprofits to work with volunteers on
particular projects. However, before I go further in detail, the glue to
all of these functionalities requires a, notification system that
contains messages, project updates, forum updates etc…

I want to help charities. Your advice and anyone else’s is greatly
appreciated – this is not a for profit venture…just trying to add a
little more good into the world :slight_smile:

On Jan 9, 2014, at 10:55 AM, Robert Jewell wrote:

t.datetime “created_at”
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:

http://guides.rubyonrails.org/association_basics.html

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

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:

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…

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:

On Jan 9, 2014, at 10:55 AM, Robert Jewell wrote:

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:

http://guides.rubyonrails.org/association_basics.html

…paying particular attention to this:

http://guides.rubyonrails.org/association_basics.html#bi-directional-associations

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.

On Jan 10, 2014, at 6:13 PM, Robert Jewell wrote:

thing for you to do is read this page:

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?

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

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.

Walter D. wrote in post #1132809:

On Jan 10, 2014, at 6:13 PM, Robert Jewell wrote:

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

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