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?
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.