Hi, i try to list all documents from MongoDB, there are 3 documents in
one collection right now.
I use presenter for formatting Json response.
So far i can list only one document from Mongo collection.
/app/controllers/v1/datas_controller.rb
class V1::DatasController < V1::ApplicationController
def index
@datas = Data.all
render json: DataPresenter.new(Data.first)
end
end
/app/presenters/presenter.rb
class Presenter
def initialize(object)
@data = object
end
def as_json
end
end
/app/presenters/data_presenter.rb
class DataPresenter < Presenter
def as_json(*)
{
id: @
data.id,
name: @
data.name,
type: @data.type,
service: @data.service
}
end
end
/app/models/data.rb
class Data
include Mongoid::Document
field :name, type: String
field :type, type: String
field :services, type: Array
end
This code list "first" document from Mongo Collection, and print it with
Json response.
What i try to do, is list all documents from Mongo Collection, how can i
do that ?
--
Posted via
http://www.ruby-forum.com/.