Hey everyone. I''m trying to create a table of "messages" for my users. It works just like an email application. Each user can send and receive messages to/from other users in the system. I''m not sure what sort of relationship to create between the user and the messages. Is it has_many or has_and_belongs_to_many? I want each message to have the following information: from to subject body status (new, read, replied, trashed) type (user_msg, system_msg, friend_request, etc.) I also want to be able to filter the messages based on their status for the user. So they can view all their sent messages, new messages, etc. I appreciate any feedback. thanks.
> I''m not sure what sort of relationship to create between the user and > the messages. Is it has_many or has_and_belongs_to_many? I want each > message to have the following information:has_many _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
I believe it should be user has_many messages message has_one user Also, you may want to rethink your statuses. A message could be both read and replied, or read and trashed. Perhaps have a flag for read/unread separate from the status. You may also want to add a created_at field for your messages to keep track of their when they occurred. And perhaps even add a parent_id for the parent message if you plan on supporting conversation chains. Tom On 12/6/05, Ramin <i8ramin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey everyone. I''m trying to create a table of "messages" for my users. > It works just like an email application. Each user can send and > receive messages to/from other users in the system. > > I''m not sure what sort of relationship to create between the user and > the messages. Is it has_many or has_and_belongs_to_many? I want each > message to have the following information: > > from > to > subject > body > status (new, read, replied, trashed) > type (user_msg, system_msg, friend_request, etc.) > > I also want to be able to filter the messages based on their status > for the user. So they can view all their sent messages, new messages, > etc. > > I appreciate any feedback. thanks. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks alot Tom. I''ll work on your suggestions. Is parent_id part of the whole ActiveRecord magic? or is it something that I have to manually set and then save when working with my model? Has anyone written a full messaging system in their application and would care to share their code? I would only use it to learn and make my own and not just copy it. Thank you On 12/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I believe it should be > > user has_many messages > message has_one user > > Also, you may want to rethink your statuses. A message could be both > read and replied, or read and trashed. Perhaps have a flag for > read/unread separate from the status. > > You may also want to add a created_at field for your messages to keep > track of their when they occurred. And perhaps even add a parent_id > for the parent message if you plan on supporting conversation chains. > > Tom > > On 12/6/05, Ramin <i8ramin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hey everyone. I''m trying to create a table of "messages" for my users. > > It works just like an email application. Each user can send and > > receive messages to/from other users in the system. > > > > I''m not sure what sort of relationship to create between the user and > > the messages. Is it has_many or has_and_belongs_to_many? I want each > > message to have the following information: > > > > from > > to > > subject > > body > > status (new, read, replied, trashed) > > type (user_msg, system_msg, friend_request, etc.) > > > > I also want to be able to filter the messages based on their status > > for the user. So they can view all their sent messages, new messages, > > etc. > > > > I appreciate any feedback. thanks. > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- - Ramin http://www.getintothis.com/blog
On 12/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I believe it should be > > user has_many messages > message has_one userThat should be message belongs_to user
Good catch Pat... my mistake. The parent_id would have to be implemented by you. The created_at is built in to RoR. Tom On 12/7/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 12/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I believe it should be > > > > user has_many messages > > message has_one user > > That should be message belongs_to user > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
One other thing, saying that he has to implement parent_id is a bit misleading. What he needs to do is put an integer field called parent_id in the messages table, and then to assign a parent to a message: messageobject.parent = parentobject Or he can access it through the Parent#messages array: parentobject.messages << messageobject Tom isn''t wrong, I just wanted to make sure that nobody misunderstands and starts doing stuff like messageobject.parent_id = parentobject.id Pat On 12/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Good catch Pat... my mistake. > > The parent_id would have to be implemented by you. The created_at is > built in to RoR. > > Tom > > On 12/7/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 12/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I believe it should be > > > > > > user has_many messages > > > message has_one user > > > > That should be message belongs_to user > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
"Tom isn''t wrong, I just wanted to make sure that nobody misunderstands and starts doing stuff like messageobject.parent_id = parentobject.id" Can someone explain to me why messageobject.parent_id = parentobject.id is incorrect? It seems to work, but I''m just curious as to why it''s bad. For example... I may have passed the parent id through the url. Seems odd that I would have to query the db to populate my parent object just to make the association to the new child object when I could just add the id to the child''s record. Of course, if there''s a good reason not to do that, I''d really be interested to know.. I want to make sure I''m doing it right! Thanks much! -Brian -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Pat Maddox Sent: Wednesday, December 07, 2005 3:12 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Need some advice on DB design One other thing, saying that he has to implement parent_id is a bit misleading. What he needs to do is put an integer field called parent_id in the messages table, and then to assign a parent to a message: messageobject.parent = parentobject Or he can access it through the Parent#messages array: parentobject.messages << messageobject Tom isn''t wrong, I just wanted to make sure that nobody misunderstands and starts doing stuff like messageobject.parent_id = parentobject.id Pat On 12/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Good catch Pat... my mistake. > > The parent_id would have to be implemented by you. The created_at is > built in to RoR. > > Tom > > On 12/7/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 12/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I believe it should be > > > > > > user has_many messages > > > message has_one user > > > > That should be message belongs_to user > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Its just not really necessary. Most of the time you are dealing with straight object (as thats the point of the Ruby and OO in general). I use it mostly when I dont have/cant represent the object in its natural state (say selecting an object from a drop down list). In these cases I stick with the ids, but if I actually have the objects I use the objects. By type more when we dont have to? :) -nick On 12/7/05, Hogan, Brian P. <HOGANBP-VnAisaAFmHY@public.gmane.org> wrote:> "Tom isn''t wrong, I just wanted to make sure that nobody misunderstands > and starts doing stuff like messageobject.parent_id = parentobject.id" > > Can someone explain to me why > > messageobject.parent_id = parentobject.id > > is incorrect? It seems to work, but I''m just curious as to why it''s bad. > For example... I may have passed the parent id through the url. Seems > odd that I would have to query the db to populate my parent object just > to make the association to the new child object when I could just add > the id to the child''s record. > > Of course, if there''s a good reason not to do that, I''d really be > interested to know.. I want to make sure I''m doing it right! > > Thanks much! > -Brian > > > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Pat Maddox > Sent: Wednesday, December 07, 2005 3:12 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] Need some advice on DB design > > > One other thing, saying that he has to implement parent_id is a bit > misleading. What he needs to do is put an integer field called > parent_id in the messages table, and then to assign a parent to a > message: > messageobject.parent = parentobject > > Or he can access it through the Parent#messages array: > parentobject.messages << messageobject > > Tom isn''t wrong, I just wanted to make sure that nobody misunderstands > and starts doing stuff like messageobject.parent_id = parentobject.id > > Pat > > > On 12/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Good catch Pat... my mistake. > > > > The parent_id would have to be implemented by you. The created_at is > > built in to RoR. > > > > Tom > > > > On 12/7/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 12/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I believe it should be > > > > > > > > user has_many messages > > > > message has_one user > > > > > > That should be message belongs_to user > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Nick: Thanks much! That makes sense and that''s exactly the guidelines that I''m using! Best, -bph -----Original Message----- From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Nick Stuart Sent: Wednesday, December 07, 2005 5:24 PM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Need some advice on DB design Its just not really necessary. Most of the time you are dealing with straight object (as thats the point of the Ruby and OO in general). I use it mostly when I dont have/cant represent the object in its natural state (say selecting an object from a drop down list). In these cases I stick with the ids, but if I actually have the objects I use the objects. By type more when we dont have to? :) -nick On 12/7/05, Hogan, Brian P. <HOGANBP-VnAisaAFmHY@public.gmane.org> wrote:> "Tom isn''t wrong, I just wanted to make sure that nobody > misunderstands and starts doing stuff like messageobject.parent_id = > parentobject.id" > > Can someone explain to me why > > messageobject.parent_id = parentobject.id > > is incorrect? It seems to work, but I''m just curious as to why it''s > bad. For example... I may have passed the parent id through the url. > Seems odd that I would have to query the db to populate my parent > object just to make the association to the new child object when I > could just add the id to the child''s record. > > Of course, if there''s a good reason not to do that, I''d really be > interested to know.. I want to make sure I''m doing it right! > > Thanks much! > -Brian > > > > > -----Original Message----- > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Pat Maddox > Sent: Wednesday, December 07, 2005 3:12 PM > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] Need some advice on DB design > > > One other thing, saying that he has to implement parent_id is a bit > misleading. What he needs to do is put an integer field called > parent_id in the messages table, and then to assign a parent to a > message: > messageobject.parent = parentobject > > Or he can access it through the Parent#messages array: > parentobject.messages << messageobject > > Tom isn''t wrong, I just wanted to make sure that nobody misunderstands> and starts doing stuff like messageobject.parent_id = parentobject.id > > Pat > > > On 12/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Good catch Pat... my mistake. > > > > The parent_id would have to be implemented by you. The created_at > > is built in to RoR. > > > > Tom > > > > On 12/7/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 12/7/05, Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I believe it should be > > > > > > > > user has_many messages > > > > message has_one user > > > > > > That should be message belongs_to user > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails