【发布时间】:2013-12-03 14:34:18
【问题描述】:
我正在尝试在我的 Sinatra 应用程序中使用 MongoMapper 来创建一个简单的拍卖网站,但在运行简单查询并获得任何结果时遇到了问题。
before '/auction/:id' do
connection = Mongo::MongoClient.new
db = connection.db('auction')
collection = db['auctions']
@query = Plucky::Query.new(collection)
end
get '/auction/:id' do
@auction = @query.where({:id => params["id"].to_i})
"#{@auction.inspect}"
end
但是我在浏览器中没有得到@auction.inspect 的输出。但是,当我使用查询 @auction = @query.all 时,我会获得数据库中的所有记录。
参数(使用 url localhost/auction/3 时):
{"splat"=>[], "captures"=>["3"], "id"=>"3"}
query.all 输出:
[{"_id"=>BSON::ObjectId('529cbbee21f7f10dd4000005'), "id"=>2, "name"=>"jump", "price"=>1.42}, {"_id"=>BSON::ObjectId('529cbbf821f7f10dd400000e'), "id"=>3, "name"=>"here", "price"=>93.34}]
【问题讨论】:
标签: ruby mongodb sinatra mongomapper