我刚开始使用MongoMapper,即使我遵循了mongomapper.com上的“指南”,在查询数据库时仍然得到了意想不到的结果。结果如下:
#<User:0x000000028d5070>
#<User:0x000000028d45a8>
#<User:0x000000029ec148>
#<User:0x000000029eb928>我的代码:
require "mongo_mapper"
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "mydb"
class User
include MongoMapper::Document
key :name, String
key :age, Integer
many :hobbies
end
class Hobby
include MongoMapper::EmbeddedDocument
key :name, String
key :started, Time
end
user = User.new(:name => 'Brandon')
user.hobbies.build(:name => 'Programming',
:started => 10.years.ago)
user.save!
puts User.where(:name => 'Brandon').all我尝试过在.where部件后面使用.first之类的东西,但仍然得到相同的结果,或者是一个勇敢的::Query对象。
发布于 2012-07-16 21:40:28
这行应该是:
puts User.where(:name => 'Brandon').first.namehttps://stackoverflow.com/questions/11503804
复制相似问题