Question

I'm trying to return a User with MongoMapper:

Here's the user

1.9.3-p194 :004 > User.where(:email => 'redacted@gmail.com').all.count
=> 1 

1.9.3-p194 :005 > User.where(:email => 'redacted@gmail.com').first.class
=> User

Looks good up to here...

1.9.3-p194 :005 > u = User.where(:email => 'redacted@gmail.com').first.class
=> User

1.9.3-p194 :007 > u.email
    NoMethodError: undefined method `email' for User:Class
    from /home/zensavona/.rvm/gems/ruby-1.9.3-p194/gems/mongo_mapper-0.12.0/lib/mongo_mapper/plugins/dynamic_querying.rb:39:in `method_missing'
    from (irb):7
    from /home/zensavona/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'

My understanding is that u holds an instance of User, but if this is so, why can't I access u's properties?

Was it helpful?

Solution

You are assigning the class User to the variable u instead of the instance of User which actually holds the data.

Try this instead:

u = User.where(:email => 'redacted@gmail.com').first
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top