Unable to check for nil result on a query

18 views
Skip to first unread message

Paolo Di Pietro

unread,
Oct 24, 2014, 4:39:54 AM10/24/14
to neo...@googlegroups.com
This is a fresh test with the 3.0.2 but preceding version had the same behavior.

    class UserIdentity
      include Neo4j::ActiveNode

     def self.from_omniauth(auth)
     
      if email = auth.info.email.nil?
        email = Devise.friendly_token[0,5] + "@" + Devise.friendly_token[0,5] + "." + Devise.friendly_token[0,3]
      end

      puts "provider: #{auth.provider} - uid: #{auth.uid} - email: #{email}"
      identity = UserIdentity.find_by(provider: auth.provider, uid: auth.uid)
      puts "identity has been found: #{identity.class} - #{identity}"

      if identity.class == nil.NilClass
        identity = UserIdentity.create(provider: auth.provider, uid: auth.uid, email: email, password: Devise.friendly_token[0,20])
        puts "identity should have been created: #{identity.class} - #{identity}"
      end
    end

As I'm running on an empty DB, the result of the find_by should be obviously 'nil'. It returns nil:NilClass or NilClass
Below, you can find the returning error:

    provider: twitter - uid: 11125452 - email: AS...@jKcZz.tbM
     CYPHER 4ms MATCH (n:`UserIdentity`) WHERE n.provider = {n_provider} AND n.uid = {n_uid} RETURN n LIMIT 1 | {:n_provider=>"twitter", :n_uid=>"11125452"}
    identity has been found: NilClass -
    Completed 500 Internal Server Error in 5ms

    NoMethodError (undefined method `NilClass' for nil:NilClass):
      app/models/user_identity.rb:108:in `from_omniauth'
      app/controllers/user_identities/omniauth_callbacks_controller.rb:33:in `set_auth'
      app/controllers/user_identities/omniauth_callbacks_controller.rb:8:in `twitter'

Brian Underwood

unread,
Oct 24, 2014, 2:49:32 PM10/24/14
to neo...@googlegroups.com
I think it's saying what the issue is... The nil object doesn't
respond to the method `NilClass`. I think you'd need to do one of the
following:

if identity.class == NilClass
if identity == nil
if identity.nil?

Brian
;p
> --
> You received this message because you are subscribed to the Google Groups
> "neo4jrb" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to neo4jrb+u...@googlegroups.com.
> To post to this group, send email to neo...@googlegroups.com.
> Visit this group at http://groups.google.com/group/neo4jrb.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages