From: Rob Sanheim Date: 2007-01-18T04:41:06+09:00 Subject: Re: ActiveRecord find_by_*() - how does it work? Hi, On 1/17/07, Eduardo Scoz wrote: > Hi guys! > > I've been trying to understand how the find_by_*() method on > ActiveRecord works, but couldn't find anything that could help. I think > that you guys can probably help me on that. > > On Active Record, you can make a call like > user.find_by_first_name_and_last_name("eduardo"...) > and this will generate the sql to send to the database. > > But how does that method gets created? ActiveRecord doesn't know what I > want to find until I actually make the call, and the number of different > combinations of parameters would probably be infinite for it to be > created during the object initialization, so (I imagine) the method must > be created __AFTER__ it was called. > > So my question is, is it possible for an object to receive a call to a > method that doesn't exist, not throw a NoMethodError, and respond to > that call? > > I hope my question makes sense! :) Its a very good question. Here are some references to help: http://blog.hasmanythrough.com/2006/08/13/how-dynamic-finders-work http://errtheblog.com/post/20 http://weblog.jamisbuck.org/2006/11/20/under-the-hood-activerecord-base-find-part-2 http://weblog.jamisbuck.org/2006/12/1/under-the-hood-activerecord-base-find-part-3 and of course, _Ruby for Rails_. - Rob