Forum: Ruby on Rails Overriding find_by_

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-11-25 18:16
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?
92926f8e4e94ac003e911c38cc4c0d0b?d=identicon&s=25 jack (Guest)
on 2005-11-25 20:01
(Received via mailing list)
List Recv 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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 technoweenie (Guest)
on 2005-11-25 20:09
(Received via mailing list)
On 11/25/05, Jack Christensen <jack@jncsoftware.com> 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
Ccfe7d097475a32dc3ff78d6fc42c852?d=identicon&s=25 List Recv (lstrecv)
on 2005-11-25 21:10
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.
92926f8e4e94ac003e911c38cc4c0d0b?d=identicon&s=25 jack (Guest)
on 2005-11-25 21:46
(Received via mailing list)
List Recv 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
This topic is locked and can not be replied to.