I''m wondering if anyone has suggestion on how to go about setting up a Model that can belong to multiple tables (but only one of them). For example, I have Companies and Contacts, both of whom can have Notes. However, I''d really rather not duplicate everything so that I have CompanyNotes and ContactNotes, so I''m wondering if there''s any way I can do the following: class Note < ActiveRecord::Base belongs_to [ [ :company, { :through => :companies_notes } ], [ :contact, :{ through => :contacts_notes } ] ] end Or if there''s any suggestions as to a better way to go about doing this. -- 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 -~----------~----~----~----~------~----~------~--~---
On Dec 22, 2007 7:32 PM, Philip Schalm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I''m wondering if anyone has suggestion on how to go about setting up a > Model that can belong to multiple tables (but only one of them). > > For example, I have Companies and Contacts, both of whom can have Notes. > However, I''d really rather not duplicate everything so that I have > CompanyNotes and ContactNotes, so I''m wondering if there''s any way I can > do the following: > > class Note < ActiveRecord::Base > belongs_to [ [ :company, { :through => :companies_notes } ], [ > :contact, :{ through => :contacts_notes } ] ] > end > > Or if there''s any suggestions as to a better way to go about doing this. >Check out polymorphic associations. Should do exactly what you''re looking for. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Note can belong to more than one model. For this u need polymorphic association. Define notable model with polymorphic as true. Other model that use has many declaration with :as notable in it Sent from my iPhone On Dec 22, 2007, at 4:32 PM, Philip Schalm <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org > wrote:> > I''m wondering if anyone has suggestion on how to go about setting up a > Model that can belong to multiple tables (but only one of them). > > For example, I have Companies and Contacts, both of whom can have > Notes. > However, I''d really rather not duplicate everything so that I have > CompanyNotes and ContactNotes, so I''m wondering if there''s any way I > can > do the following: > > class Note < ActiveRecord::Base > belongs_to [ [ :company, { :through => :companies_notes } ], [ > :contact, :{ through => :contacts_notes } ] ] > end > > Or if there''s any suggestions as to a better way to go about doing > this. > -- > 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 -~----------~----~----~----~------~----~------~--~---
Michael Guterl wrote:> On Dec 22, 2007 7:32 PM, Philip Schalm > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > >> belongs_to [ [ :company, { :through => :companies_notes } ], [ >> :contact, :{ through => :contacts_notes } ] ] >> end >> >> Or if there''s any suggestions as to a better way to go about doing this. >> > > Check out polymorphic associations. Should do exactly what you''re > looking > for.Sweet, thanks! -- 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 -~----------~----~----~----~------~----~------~--~---
On Dec 22, 2007, at 6:32 PM, Philip Schalm wrote:> I''m wondering if anyone has suggestion on how to go about setting up a > Model that can belong to multiple tables (but only one of them). > > For example, I have Companies and Contacts, both of whom can have > Notes. > However, I''d really rather not duplicate everythingI have almost exactly that. Here''s the migration: ------------------ class CreateNotes < ActiveRecord::Migration def self.up create_table :notes do |t| t.column :notable_id, :integer #required t.column :notable_type, :string #required t.column :created_at, :datetime t.column :processed_by, :string, :limit => 20 t.column :body, :text t.column :title, :string, :limit => 50 t.column :department, :string, :limit => 30 end end The necessary columns are the id and type. And the Note class -------------------------- class Note < ActiveRecord::Base belongs_to :notable, :polymorphic => true end And then for any class you want to have notes, just make it look like this: ----------------------------------- class Client < ActiveRecord::Base has_many :notes, :as => :notable end And that''s it. Now you can simply write something like this: client = Client.find(1) #for example note = Note.new note.title = "the title" note.body = "the body" note.department = ''some department'' note.processed_by = current_user_name client.notes << note And with that last line the note is magically associated with that client, and is part of its notes array. other than adding the has_many :notes, :as => :notable line, you don''t have to do anything at all to the models you want notes for. It''s been a while since I''ve even thought about this, but I don''t think I''ve forgotten anything. Hope this helps. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Is there no join table to tie the two together? Or is that all done automagically somehow... -- 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 -~----------~----~----~----~------~----~------~--~---
Philip Schalm wrote:> Is there no join table to tie the two together? Or is that all done > automagically somehow...Cancel that! Found out how it works in the Agile Web Dev w/Rails book :) Thanks everyone! -- 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 -~----------~----~----~----~------~----~------~--~---