Hello, so I have a situation that I''m finding hard to model. I have two objects, User and Message. Here''s a condensed version of what they look like below: User: ----- id: integer name: string Message: -------- id: integer from_id: integer (should point to a user) to_id: integer (should point to a user) body: string I''m working on my tests and have my fixture data all in place. My first test goes something like this: m = Message.find(1) assert m.to_user.id==2 assert m.from_user.id==1 db table looks like (hope the ''formatting'' works): Messages ------------------------------- | id | to_id | from_id | body | ------------------------------- | 1 | 2 | 1 | blah | | 2 | 1 | 2 | foo | ------------------------------- The user table is should be self-explanatory. I also have no foreign keys specifically set as I read in various blog postings that this was not necessary from a rails standpoint and I have other models & relationships working fine w/out it. My big prob is I''m not able to figure out the right relationship type: messages belongs_to user and user has many message_tos? That kind of stuff. This is a fairly simple association and I dunno why I''m having so many probs w/it. Any suggestions? Been going round with this thing for hours now. Tia! -j -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Jason, I listened to David Heinemeier Hansson''s keynote speech from the RailsConf2006 recently and it addressed this very problem! That is, that there''s some missing piece in the model and if you think in terms of simple CRUD operations on each piece of it you can come up with the missing piece and maybe even insight into further aspects of your app. A video of his talk is at http://blog.scribestudio.com/articles/2006/07/09/david-heinemeier-hansson-railsconf-2006-keynote-address and the slides are at http://www.loudthinking.com/lt-files/worldofresources.pdf. Anyway, I tried to apply his principles to your model and here''s my suggestion. You may come up with an even better one after further reflection. How about only including the sender''s user_id in the messages table and adding a new table named something like MsgAddressees? A message may be addressed to more than one person, at least in most situations I know of. So you would have Message: -------- id: integer user_id: integer (should point to the user who sent the message) body: string MsgAddressee: -------- id: integer message_id: integer (should point to a message) user_id: integer (should point to a user who the message was sent to) Then you can have a User has_many Messages and a Message belongs_to a User but has_many MsgAddresses. Your situation may be more complex but I hope this helps you through the logjam. I highly recommend David''s talk! Shauna -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Shauna and thx for the reply! Yah, I watched DH''s keynote - always inspiring. However, I forgot some of the things you mentioned. And the more I look at it, the more I like yr take on it. I suppose I was looking at it very one-sided and really, I put this out there for another point of view. I think your approach works better and satisfies the thing that stuck in my head most which was how to get to a ''to'' msg out the quickest. But I suppose, I can query the addresse table just fine in those cases and work my way up the graph and back. So this is good. Anyway, thx again! I''m gonna go back and re-visit the talk tonight. :) -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shauna''s reply is efficient if you want to model messages being sent to multiple people, but I am not sure it answers your immediate question. Given your database schema the models should look something like: User --- has_many :sent_messages, :classname => "Message", :foreign_key => :from_id has_many :received_messages, class_name => "Message", :foreign_key => :to_id Messages --- belongs_to :sender, :classname => "User", :foreign_key => :from_id belongs_to :receiver, classname => "User", :foreign_key => :to_id On Dec 19, 10:41 am, Jason <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello, > > so I have a situation that I''m finding hard to model. I have two > objects, User and Message. Here''s a condensed version of what they look > like below: > > User: > ----- > id: integer > name: string > > Message: > -------- > id: integer > from_id: integer (should point to a user) > to_id: integer (should point to a user) > body: string > > I''m working on my tests and have my fixture data all in place. My first > test goes something like this: > > m = Message.find(1) > assert m.to_user.id==2 > assert m.from_user.id==1 > > db table looks like (hope the ''formatting'' works): > > Messages > ------------------------------- > | id | to_id | from_id | body | > ------------------------------- > | 1 | 2 | 1 | blah | > | 2 | 1 | 2 | foo | > ------------------------------- > > The user table is should be self-explanatory. I also have no foreign > keys specifically set as I read in various blog postings that this was > not necessary from a rails standpoint and I have other models & > relationships working fine w/out it. > > My big prob is I''m not able to figure out the right relationship type: > messages belongs_to user and user has many message_tos? That kind of > stuff. This is a fairly simple association and I dunno why I''m having so > many probs w/it. > > Any suggestions? Been going round with this thing for hours now. Tia! > > -j > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi -- On Tue, 19 Dec 2006, Jason wrote:> > Hello, > > so I have a situation that I''m finding hard to model. I have two > objects, User and Message. Here''s a condensed version of what they look > like below: > > User: > ----- > id: integer > name: string > > Message: > -------- > id: integer > from_id: integer (should point to a user) > to_id: integer (should point to a user) > body: string > > I''m working on my tests and have my fixture data all in place. My first > test goes something like this: > > m = Message.find(1) > assert m.to_user.id==2 > assert m.from_user.id==1 > > db table looks like (hope the ''formatting'' works): > > Messages > ------------------------------- > | id | to_id | from_id | body | > ------------------------------- > | 1 | 2 | 1 | blah | > | 2 | 1 | 2 | foo | > ------------------------------- > > The user table is should be self-explanatory. I also have no foreign > keys specifically set as I read in various blog postings that this was > not necessary from a rails standpoint and I have other models & > relationships working fine w/out it. > > My big prob is I''m not able to figure out the right relationship type: > messages belongs_to user and user has many message_tos? That kind of > stuff. This is a fairly simple association and I dunno why I''m having so > many probs w/it. > > Any suggestions? Been going round with this thing for hours now. Tia!If you''re in the market for modifying the schema, you might want to do what''s described here: http://dablog.rubypal.com/articles/2006/06/25/modeling-many-to-many-i-guess-i-was-right-the-first-time Basically the model I used involved the notion of a Correspondence. Every message belongs to a correspondence. Every message also belongs to a sender and a receiver. This might run aground on messages sent to many people (the whole thrust of the application I was working on was private exchanges), but it''s another example of how one might think in terms of an intermediate class and the CRUD alignment that might bring about. David -- Q. What''s a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hello, I''m building a contact management system for a small school and I''m having trouble coming up with the right data-model that works well with Rails. Here''s the mysql contacts id first_name last_name ... donors id contact_id status ... faculty id contact_id hire_date directors id contact_id The contact table contains basic stuff that all contacts will have, and subsequent models extend Contact to add additional data. Some of the sub models are Donor, Faculty, Director. A single Contact can be a Donor, Faculty, and a Director, so it''s what I think is called multi-table inheritance which isn''t really supported in Rails. Some of the workarounds I''ve seen seemed overly complex. I was planning to do something like this: class Donor < ActiveRecord::Base has_one :contact end for all the sub models and I think that would work, but I can''t use the syntax Donor.find(1, :include => :contact) because this requires a donor_id column in contacts, and I doesn''t seem to be good to have donor_id, faculty_id, director_id, foreign key columns in contacts (maybe this isn''t a problem??) Also, assuming the above, when I create one of the sub models I can''t get the contact_id to save to donor automatically. @contact = Contact.new(params[:contact]) @donor = Donor.new(params[:donor]) Donor.transaction do @contact.save @donor.contact_id = @contact.id if @donor.save flash[:notice] = ''Donor was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end I can get the schema above to work, but I think I''ll have to do a lot of custom sql to make it work rather than using the built-in :include. Unless there''s a way to "reverse" the underlying join of the has_one relationship so a contact_id in donors joins to contacts? Thanks for the help! Mark --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Just reverse the relationship, as you suggested: class Donor < ActiveRecord::Base belongs_to :contact end class Contact < ActiveRecord::Base has_one :donor has_one :faculty has_one :director end Rails will then use the contact_id field in Donor, Faculty etc. and you won''t need the donor_id and other id fields in Contact. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hello again, I''ve been thinking about this some more and I think the easiest thing to do is to put foreign key columns in contacts for each of the sub-models. While this would require a schema change in contacts for every "new" sub-model, that''s okay for this project. By doing this, I''m not polluting the elegant natural language of rails. The Donor can :has_one Contact, and I can do a Donor.find(1, :include => :contact). The following code worked great:>> c = Contact.new >> d = Donor.new >> d.contact = c >> d.save >> c.donor_id=> 1>> Donor.find(1, :include => :contact)Regardless, I''m still looking forward to other ideas. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You''re completely correct, I learned that this would work after my first post, but I was conflicted in saying that a Donor belongs to a Contact, because it didn''t really describe the relationship. Anyway, thanks! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You really shouldn''t get caught up with the descriptions "has_one" and "belongs_to". In rails they mean little more than "I don''t have the foreign key" and "I do have the foreign key". There''s no reason why the item that "belongs to" another item needs to have the foreign key. And rails will work quite happily either way. All the code you used in your example will work identically if you reverse the relationship (except for c.donor_id, obviously). In particular, the ":include => contact" will still work with the relationship reversed. I''ve used this "reverse relationship" in a couple of places in my current project because it simply makes more sense that way. And rails hasn''t complained once. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
mveerman wrote:> You''re completely correct, I learned that this would work after my > first post, but I was conflicted in saying that a Donor belongs to a > Contact, because it didn''t really describe the relationship. Anyway, > thanks!You could also look into either using Single Table Inheritence (STI) as the fan out of the subclasses isn''t too high, or use a polymorphic association. -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---