Ruby on Rails Tuesday, April 28, 2015

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/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/53e7fbe0db05b1da11ce3d57cd4bd673%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment