On Wed, May 30, 2007 at 08:56:08PM +0900, Maurice Gladwell wrote:
> Of course, we don't have standard support for that currently, but we 
> could (e.g. with some DSL) declare inside the class definition (e.g. 
> above the `def method_missing`) patterns to which method_missing would 
> respond.
> 
> So for example, in ActiveRecord::Base we would have something like:
> 
> responds_to /find_by_.+/

Hmm, everybody forgets to anchor their regular expressions :-)

Anyway, this doesn't seem right. find_by_foo will fail if there is no column
called 'foo'. So really you should really check that the column exists.

But that doesn't work either, when you remember that AR also supports
find_by_foo_and_bar(x,y), find_by_foo_and_bar_and_baz(x,y,z) etc.

It would be too expensive to enumerate all these possibilities up-front.
Rather, I think you need to check whether a particular method name is valid
or not. In which case, what you really want is to define a respond_to?
method in ActiveRecord::Base which has the same logic as 'find'.

class ActiveRecord::Base
  alias :respond_to :real_respond_to
  def respond_to?(m)
    return true if real_respond_to?(m)

    # now also return true if m is of the form find_xxxxxxxx and
    # xxxxxx is a valid finder pattern
  end
end

Regards,

Brian.