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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Robert Walker at Oct 2, 2011 at 4:12 am

    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

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to [email protected].
    To unsubscribe from this group, send email to [email protected].
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedOct 1, '11 at 2:36p
activeOct 2, '11 at 4:12a
posts2
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Robert Walker: 1 post Frederiko Costa: 1 post

People

Translate

site design / logo © 2023 Grokbase