G
gwtmp01
Are you saying that when Model.find_by_city is executed, the model
looks at
the database, discovers there's a city column, and on the fly
constructs a
method to search the table by city?
I think Hal already answered this, but yes. If your class defines
the instance method 'method_missing' then Ruby will call it when it
can't
find a method definition. It would look something like this (not
tested):
class Model
def method_missing(method_name, *args)
if method_name.to_s =~ /^find_by_(.*)/
# respond to find_by call
else
super # results in standard method not found exception
end
end
end
(Note that the first argument to method_missing is an instance of
Symbol, not
String so you need to call to_s on it in order to compare it to the
regular expression. What is the difference between a Symbol and String
you ask? Um, read the archives.)
This feature of Ruby's method dispatch protocol allows a class to
implement methods on demand. It can even install a definition for
the missed method so that the *next* time it will be called directly
instead of being intercepted by method_missing.
Very handy.
Gary Wright