REST API - MongoDB and Json Response

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 ?

On Tue, Apr 28, 2015 at 5:14 AM, Sajmon S. [email protected]
wrote:

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 ?

Are you unclear on what methods an Array responds to? (hint: docs)

Or is the question something else?


Hassan S. ------------------------ [email protected]
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Hi Hassan,

can You show code example ?

Thanks

On Tue, Apr 28, 2015 at 12:23 PM, Sajmon S. [email protected]
wrote:

can You show code example ?

I’m not sure what you’re looking for. Again, do you not know Array
methods? Have you read the doc ?

If not, start here: http://ruby-doc.org/core-2.2.2/Array.html


Hassan S. ------------------------ [email protected]
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

On 28 April 2015 at 21:07, Sajmon S. [email protected] wrote:

But then i have exception.
I suggest that you start by working right through a good tutorial such
as railstutorial.org (which is free to use online), including doing
all the exercises. That will show you the basics of Rails and Ruby.
Otherwise you will later look back at this thread and be embarrassed.

Colin

Are You sure, Your tutorial shows me how to handle multiple mongo
documents via presenter ?

You wanna help? Write some code example.

As I wrote earlier, i try to get all documents from MongoDB collection.

In the above code example, You have:

render json: DataPresenter.new(Data.first)

I try:

render json: DataPresenter.new(Data.all)

But then i have exception.

So now You know what Im try to do, maybe You can show some code
example.

Thanks.

On 28 April 2015 at 21:25, Sajmon S. [email protected] wrote:

Are You sure, Your tutorial shows me how to handle multiple mongo
documents via presenter ?

What you are looking for is not how to handle multiple mongo documents
via presenter. What you are looking for is a basic understanding of
Rails and Ruby. Without that you have no prospect of succeeding.

You wanna help? Write some code example.

You need to help yourself first. If someone supplies you with the
answer to this specific question then you will soon come back with
another simple problem that you should be able to solve yourself.
When you have worked right through the tutorial (take several days at
it, the time will not be wasted, it will repay itself very rapidly)
then if you still cannot solve the problem come back again. I will be
most surprised if you find that necessary though.

Colin

On Tue, Apr 28, 2015 at 1:07 PM, Sajmon S. [email protected]
wrote:

Some now You know what Im try to do, maybe You can show some code
example.

I know what you’re trying to do; I don’t know why you’re having a
problem with it, unless you don’t know how to work with an Array.

Hence my suggestion that you read the doc, which also happens
to contain “code examples” :slight_smile:


Hassan S. ------------------------ [email protected]
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Thanks for speech,

Maybe someone alese.

On 28 April 2015 at 21:39, Sajmon S. [email protected] wrote:

Thanks for speech,

Glad to be of help

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs