Private Message System Rails 4

78 views
Skip to first unread message

Robert Jewell

unread,
Jan 9, 2014, 9:54:21 AM1/9/14
to rubyonra...@googlegroups.com
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?

--
Posted via http://www.ruby-forum.com/.

Walter Lee Davis

unread,
Jan 9, 2014, 10:10:18 AM1/9/14
to rubyonra...@googlegroups.com

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

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/286222e2a96ec7bca78d4b13344726b0%40ruby-forum.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Robert Jewell

unread,
Jan 9, 2014, 10:55:54 AM1/9/14
to rubyonra...@googlegroups.com
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 :-)

Robert Jewell

unread,
Jan 9, 2014, 11:02:12 AM1/9/14
to rubyonra...@googlegroups.com
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...

Walter Lee Davis

unread,
Jan 9, 2014, 12:19:23 PM1/9/14
to rubyonra...@googlegroups.com

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

> 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:

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

>
> 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 :-)
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/47064f0a45aa2022ab1788ff2cc4d1b3%40ruby-forum.com.

Robert Jewell

unread,
Jan 10, 2014, 6:13:04 PM1/10/14
to rubyonra...@googlegroups.com
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 :-)

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.

Walter Lee Davis

unread,
Jan 10, 2014, 6:38:38 PM1/10/14
to rubyonra...@googlegroups.com

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

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

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/adc9cfb81c2dd11fab9b5db2925e4856%40ruby-forum.com.

Robert Jewell

unread,
Jan 11, 2014, 12:58:50 PM1/11/14
to rubyonra...@googlegroups.com
Walter Davis 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

Robert Jewell

unread,
Jan 11, 2014, 2:50:56 PM1/11/14
to rubyonra...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages