Find_by_id and find_by_email methods are not available to me

Hello everyone,

I’m learning Rails through the railtutorial.org book. I’m following very
closely every detail taught in the book. However, there’s something that
I
don’t seem to find a solution. I have the following snippet of code:

def authenticate(email, submitted_password)
user = find_by_email(email)
#user = where(:email => email).first
(user && user.has_password?(submitted_password)) ? user : nil
end

def authenticate_with_salt(id, cookie_salt)
  user = find_by_id(id)
  #user = where(:id => id)
  (user && user.salt == cookie_salt) ? user : nil
end

end

in the authenticate method, the find_by_id(id) call is returning nil.
Going
to the console, the function is not available to me whenever I call it.
The
funny thing is that it does not throw an exception (it’s heavily using
meta-programming), so the method is generated on the fly, I guess. The
thing
is: the method is being called, but always returns nil. Does anyone know
why
is this happening? I had to replace the code by user = where(:email =>
email).first, for example. It works flawlessly. Can it be something
related
to the Ruby version I’m using?

$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.5.0]

$ rails -v
Rails 3.0.9

$ gem list activerecord
activerecord (3.0.9, 3.0.1)
I appreciate any help …

Thanks,
-fred

Frederiko Costa wrote in post #1024504:

def authenticate(email, submitted_password)
user = find_by_email(email)
#user = where(:email => email).first
(user && user.has_password?(submitted_password)) ? user : nil
end

in the authenticate method, the find_by_id(id) call is returning nil.
Going
to the console, the function is not available to me whenever I call it.
The

If you have:

user = find_by_id(id)

What part of that statement is the receiver of the message “find_by_id”?

I think you meant:

user = User.find_by_id(id)

So to answer my own question, the receiver in this case is the class
User, which means that find_by_id is a class method as opposed to an
instance method. And, just to be clear, the method “find_by_id” is what
is called a “dynamic finder” method. It actually doesn’t exist until the
first time it is called. The method will be dynamically added to the
receiving class (User in this case) via the method_missing method that
all Ruby objects respond to.

http://blog.hasmanythrough.com/2006/8/13/how-dynamic-finders-work