Thursday, January 9, 2014

Re: [Rails] Private Message System Rails 4

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-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/47064f0a45aa2022ab1788ff2cc4d1b3%40ruby-forum.com.
> For more options, visit https://groups.google.com/groups/opt_out.

--
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-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/893C28C9-3EAC-4F58-BF84-396D1213C24B%40wdstudio.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate