Note belongs_to Company or Contact?

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.

On Dec 22, 2007 7:32 PM, Philip S.
[email protected]
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.

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 S.
<[email protected]

Michael G. wrote:

On Dec 22, 2007 7:32 PM, Philip S.
[email protected]
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!

On Dec 22, 2007, at 6:32 PM, Philip S. 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

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

Philip S. 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
:slight_smile: Thanks everyone!

Is there no join table to tie the two together? Or is that all done
automagically somehow…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs