【问题标题】:MongoMapper getting data from a queryMongoMapper 从查询中获取数据
【发布时间】:2013-05-06 03:47:17
【问题描述】:

我正在尝试使用 MongoMapper 返回 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

这里看起来不错...

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>'

我的理解是u拥有User的一个实例,但如果是这样,为什么我不能访问u的属性?

【问题讨论】:

    标签: ruby mongodb sinatra mongomapper


    【解决方案1】:

    您将User 类分配给变量u,而不是实际保存数据的User 实例。

    试试这个:

    u = User.where(:email => 'redacted@gmail.com').first
    

    【讨论】:

    • 谢谢!今天下午我没有想!
    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    相关资源
    最近更新 更多