0

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 :)

matt
  • 78,533
  • 8
  • 163
  • 197
Darcbar
  • 888
  • 1
  • 8
  • 25

2 Answers2

0

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

Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
0

Find method return an enumerator not the entry. If you expect only ONE entry : try this

Post.first(params[:id]).to_json
germanlinux
  • 2,501
  • 1
  • 20
  • 8