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
I’ve thought of a few hacks to handle this, but I’m looking for a more
elegant, integrated solution. Any ideas?