# Note belongs\_to Company or Contact?

**URL:** <https://www.ruby-forum.com/t/note-belongs-to-company-or-contact/123981>\
**Category:** Rails\
**Created:** [December 23, 2007, 1:32am UTC](https://www.ruby-forum.com/t/note-belongs-to-company-or-contact/123981 "2007-12-23T01:32:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pnomolos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@pnomolos](https://www.ruby-forum.com/u/pnomolos)\
**Post date:** [December 23, 2007, 1:32am UTC](https://www.ruby-forum.com/t/note-belongs-to-company-or-contact/123981/1 "2007-12-23T01:32:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pnomolos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@pnomolos](https://www.ruby-forum.com/u/pnomolos)\
**Post date:** [December 23, 2007, 2:12am UTC](https://www.ruby-forum.com/t/note-belongs-to-company-or-contact/123981/2 "2007-12-23T02:12:34Z")

</div>

On Dec 22, 2007 7:32 PM, Philip S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

---

<div class="post-metadata">

**Author:** ![pnomolos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@pnomolos](https://www.ruby-forum.com/u/pnomolos)\
**Post date:** [December 23, 2007, 2:17am UTC](https://www.ruby-forum.com/t/note-belongs-to-company-or-contact/123981/3 "2007-12-23T02:17:49Z")

</div>

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.  
\<removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![pnomolos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@pnomolos](https://www.ruby-forum.com/u/pnomolos)\
**Post date:** [December 23, 2007, 6:15am UTC](https://www.ruby-forum.com/t/note-belongs-to-company-or-contact/123981/4 "2007-12-23T06:15:38Z")

</div>

Michael G. wrote:

> On Dec 22, 2007 7:32 PM, Philip S.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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!

---

<div class="post-metadata">

**Author:** ![pnomolos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@pnomolos](https://www.ruby-forum.com/u/pnomolos)\
**Post date:** [December 23, 2007, 6:56am UTC](https://www.ruby-forum.com/t/note-belongs-to-company-or-contact/123981/5 "2007-12-23T06:56:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pnomolos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@pnomolos](https://www.ruby-forum.com/u/pnomolos)\
**Post date:** [December 24, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/note-belongs-to-company-or-contact/123981/6 "2007-12-24T18:01:08Z")

</div>

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  
🙂 Thanks everyone!

---

<div class="post-metadata">

**Author:** ![pnomolos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/85f322/32.png) [@pnomolos](https://www.ruby-forum.com/u/pnomolos)\
**Post date:** [December 24, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/note-belongs-to-company-or-contact/123981/7 "2007-12-24T17:47:10Z")

</div>

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