# Overriding find

**URL:** <https://www.ruby-forum.com/t/overriding-find/133226>\
**Category:** Rails\
**Created:** [March 29, 2008, 2:27am UTC](https://www.ruby-forum.com/t/overriding-find/133226 "2008-03-29T02:27:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [March 29, 2008, 2:27am UTC](https://www.ruby-forum.com/t/overriding-find/133226/1 "2008-03-29T02:27:17Z")

</div>

Hi,

I am using single table inheritance which looks something like this:

class Employee \< ActiveRecord::Base  
belongs\_to :employee\_type  
end  
Manager \< Employee  
Programmer \< Employee

When I call Manager.find(…) or Programmer.find(…) regardless of  
what parameters are passed to find I only want that employee type to  
be returned. So Prgrammers.find(:all) will return all programmers, but  
not managers.

I somehow need to inject a condition “member\_type = xxx” into every  
find query of the Employee subclass.

I would like to be able to do it without any restrictions on calling  
find. So find(:first), find(id) and find(:all) will all work.

Has anyone done this before or can point me in the right direction?

Regards

Will

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [March 29, 2008, 2:35am UTC](https://www.ruby-forum.com/t/overriding-find/133226/2 "2008-03-29T02:35:31Z")

</div>

Will wrote:

> be returned. So Prgrammers.find(:all) will return all programmers, but  
> not managers.

Google [rails polymorphic association]!

–  
Phlip

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [March 29, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/overriding-find/133226/3 "2008-03-29T17:35:19Z")

</div>

Will,

It looks like STI is what you’re using and what you want (assuming  
your data lives in one table). Looking at the AWDwRoR book, it  
doesn’t seem like you need the belongs\_to :employee\_type in your  
Employee class unless it’s related to something else in your app.  
Also, I’m assuming that you have abbreviated your Manager and  
Programmer classes and that they actually look like this:

class Manager \< Employee  
end

class Programmer \< Employee  
end

What does the data in your employees table look like? Do you have a  
“type” column that stores either “Manager” or “Programmer” in it? I  
haven’t used STI before, but based on the examples that I’ve seen, it  
should work as you want it to as long as it’s set up correctly.

-Kyle

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [March 29, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/overriding-find/133226/4 "2008-03-29T18:49:25Z")

</div>

What Kyle said. 🙂

create\_table :employees do |t|  
t.string :type, :default =\> “Employee”, :null =\> false  
t.string :name  
(… other fields …)  
end

Then all you need is the model extensions like Kyle wrote:

class Employee \< ActiveRecord::Base; end  
class Manager \< Employee; end  
class Programmer \< Employee; end

And you can do stuff like:

managers = Manager.find :all  
best\_programmers = Programmer.find\_all\_by\_language(“Ruby on Rails”)  
all\_employees = Employee.find :all

-Danimal

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [March 30, 2008, 9:11am UTC](https://www.ruby-forum.com/t/overriding-find/133226/5 "2008-03-30T09:11:06Z")

</div>

Thanks all for your replies. The type field will sort this out.
