I am trying to do the following in a sinatra route:
get '/posts/:id' do
Post.find(params[:id]).to_json
end
But this is returning an enumerator.
How do I access a single object in json format?
PS I’m using datamapper
EDIT
I managed to return the json value by using get instead of find:
get '/posts/:id' do
Post.get(params[:id]).to_json
end
If someone can explain why I will accept answer so not to waste the question 🙂
finddoesn’t exist in DataMapper. It’s a method of Ruby’s Enumerable, that returns an enumerator object ; which you’re trying to convert to JSON.DataMapper objects apparently implement Enumerable, that’s why you don’t get an undefined method exception.
Enumerable#find: http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-find