# Find-all with polymorphic association?

**URL:** <https://www.ruby-forum.com/t/find-all-with-polymorphic-association/103558>\
**Category:** Rails\
**Created:** [May 7, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/find-all-with-polymorphic-association/103558 "2007-05-07T22:18:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 7, 2007, 10:18pm UTC](https://www.ruby-forum.com/t/find-all-with-polymorphic-association/103558/1 "2007-05-07T22:18:00Z")

</div>

I have this:

class Document \< ActiveRecord::Base

belongs\_to :documentable, :polymorphic =\> true

and this:

class Task \< ActiveRecord::Base

has\_many :documents, :as =\> :documentable

This, on Document, doesn’t work:

def self.find\_all\_by\_documentable\_id(id)  
Document.find\_all\_by\_documentable(id)  
end

undefined method `find\_all\_by\_documentable’ for Document:Class

I’ve spent hours googling on polymorphism and find, and everyone just  
talks about how it just works. Only it doesn’t because find\_all\_by  
isn’t defined for my polymorphic association. I guess I could do

.find(“documentable\_type = ?, documentable\_id = ?”…

but that just seems really crude. Any advice?

---

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 7, 2007, 10:57pm UTC](https://www.ruby-forum.com/t/find-all-with-polymorphic-association/103558/2 "2007-05-07T22:57:07Z")

</div>

There is no documentable field defined on Document but  
documentable\_id, which means you don’t need to define your own method,  
just call Document.find\_all\_by\_documentable\_id(some\_id). Hope it  
answers your question.

Val  
[http://revolutiononrails.blogspot.com/](http://revolutiononrails.blogspot.com/)

---

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 7, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/find-all-with-polymorphic-association/103558/3 "2007-05-07T22:56:07Z")

</div>

Why is Documentable another model? Why is it not a column n the  
Document table?

Zach I.  
â†’ Blog â€” [http://www.zachinglis.com](http://www.zachinglis.com)  
â†’ Company â€” [http://www.lt3media.com](http://www.lt3media.com)  
â†’ Portfolio â€” [http://portfolio.zachinglis.com](http://portfolio.zachinglis.com)

---

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 8, 2007, 12:40am UTC](https://www.ruby-forum.com/t/find-all-with-polymorphic-association/103558/4 "2007-05-08T00:40:24Z")

</div>

There is a documentable field for most purposes eg I can do

d = Document.find(1)  
p d.documentable

It seems odd that I don’t get find methods to go along with this. Is  
that really the case?

---

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 8, 2007, 2:38am UTC](https://www.ruby-forum.com/t/find-all-with-polymorphic-association/103558/5 "2007-05-08T02:38:30Z")

</div>

You could do that without the extra documentable field.

class Document \< ActiveRecord::Base

#update documentable to true  
def documentable  
update\_attributes(:documentable =\> 1)  
end

#update documentable to false  
def not\_documentable  
update\_attributes(:documentable =\> 0)  
end

# check to see if it’s true or false

def documentable?  
case self.documentable  
where 0  
“is NOT documentable”  
where 1  
“is documentable”  
end  
end

Something like that. and only with a documentable field rather than a  
polymorphic association which I could only presume would be heavier.

Zach I.  
â†’ Blog â€” [http://www.zachinglis.com](http://www.zachinglis.com)  
â†’ Company â€” [http://www.lt3media.com](http://www.lt3media.com)  
â†’ Portfolio â€” [http://portfolio.zachinglis.com](http://portfolio.zachinglis.com)

---

<div class="post-metadata">

**Author:** ![Gisborne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/dc4da7/32.png) [@Gisborne](https://www.ruby-forum.com/u/Gisborne)\
**Post date:** [May 8, 2007, 8:04am UTC](https://www.ruby-forum.com/t/find-all-with-polymorphic-association/103558/6 "2007-05-08T08:04:48Z")

</div>

It’s an association, so you don’t get find\_all\_by\_documentable for the  
same reason you don’t get find\_all\_by\_normal\_belongs\_to\_association.

In other words, given the class below, you don’t get find\_all\_by\_foo.  
You only get find\_all\_by\_foo\_id.

class Bar \< ActiveRecord::Base  
belongs\_to :foo  
end
