# Overriding find\_by\_

**URL:** <https://www.ruby-forum.com/t/overriding-find-by/48854>\
**Category:** Rails\
**Created:** [November 25, 2005, 6:16pm UTC](https://www.ruby-forum.com/t/overriding-find-by/48854 "2005-11-25T18:16:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 25, 2005, 6:16pm UTC](https://www.ruby-forum.com/t/overriding-find-by/48854/1 "2005-11-25T18:16:50Z")

</div>

In one subclass of an ActiveRecord based model (using Single Table  
Inheritance), I override one of the attributes to be calculated, as  
opposed to looking at the data stored in the database. (Example:  
Product’s have a price field stored in the DB, but PerishableProduct’s  
price field is calculated based on other factors…).

So far, so good.

_But_, I’m concerned about Product.find\_by\_price. I know that this uses  
SQL to find things, which, in this case, will be wrong. I’d like to  
override .find\_by\_price, to do the search in Ruby (and not in SQL), so  
that it can check the calculated field. But, this is complicated,  
because a) find\_by\_price is a generated method and b) there are other  
combinations: find\_by\_color\_and\_price .

I could just avoid using that method, but since other people will be  
working on that code later, I’m concerned that someone won’t realize and  
will use it. Besides, I’d like to use consistent names for consistent  
ideas, even if the implementation changes (isn’t that what OO is  
about?).

I’ve thought of a few hacks to handle this, but I’m looking for a more  
elegant, integrated solution. Any ideas?

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 25, 2005, 8:01pm UTC](https://www.ruby-forum.com/t/overriding-find-by/48854/2 "2005-11-25T20:01:12Z")

</div>

List R. wrote:

> override .find\_by\_price, to do the search in Ruby (and not in SQL), so  
> I’ve thought of a few hacks to handle this, but I’m looking for a more  
> elegant, integrated solution. Any ideas?

Try overriding method\_missing. If it is a find\_by\_price variant raise an  
exception. If not call super.

Jack

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 25, 2005, 8:09pm UTC](https://www.ruby-forum.com/t/overriding-find-by/48854/3 "2005-11-25T20:09:17Z")

</div>

On 11/25/05, Jack C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > _But_, I’m concerned about Product.find\_by\_price. I know that this uses  
> > about?).
> > 
> > I’ve thought of a few hacks to handle this, but I’m looking for a more  
> > elegant, integrated solution. Any ideas?
> 
> Try overriding method\_missing. If it is a find\_by\_price variant raise an  
> exception. If not call super.

I think I would refrain from messing with method\_missing:

class Foo \< AR::Base  
def self.find\_by\_price  
raise “Don’t use this method!!!”  
end  
end

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 25, 2005, 9:10pm UTC](https://www.ruby-forum.com/t/overriding-find-by/48854/4 "2005-11-25T21:10:59Z")

</div>

technoweenie wrote:

> class Foo \< AR::Base  
> def self.find\_by\_price  
> raise “Don’t use this method!!!”  
> end  
> end

I thought of this, but what about all the permutations:  
find\_by\_price\_and\_color, etc.

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [November 25, 2005, 9:46pm UTC](https://www.ruby-forum.com/t/overriding-find-by/48854/5 "2005-11-25T21:46:06Z")

</div>

List R. wrote:

> I thought of this, but what about all the permutations:  
> find\_by\_price\_and\_color, etc.

That’s why I suggested overriding method\_missing.

Untested – but should work

def self.method\_missing( method\_id, \*args )  
if method\_id.to\_s.include?(‘find\_by’) &&  
method\_id.to\_s.include?(‘price’)  
raise ‘Don’t use this.’  
else  
super  
end  
end

Jack
