0

I'm a newbie on RoR (and Ruby). I need a little help about a json response (with Grape).

This is the sample:

    {
      events: [
           {
            'some data':'some data',
             place_id: 1
           }
      ]
    }

Now this is the result of Events.all in Rails, but I want to make for each event a query for the place, to have more data instead only id. I'm sure that new lambda function can help me, but for now I have no idea about to make it. I'm trying without success...

Thanks in advance

UPDATE

Desired result

{
          events: [
               {
                'some data':'some data',
                 place : {
                   id: 1,
                   name: 'Blablabla'
               }
          ]
        }
Luis C.
  • 727
  • 3
  • 22
  • 34
  • 1
    So you want a function that gives you a list of all the places for a list of events? from where/what service do you want to query it? are you saying that given the place_id you then want to query for the place? – Mike H-R Apr 16 '14 at 18:19
  • Exactly... The results that you can see came from Events.all. Now for each event I want to call Place.find(event.place_id), to add more detail about places, for each element of array events. – Luis C. Apr 17 '14 at 07:17

2 Answers2

2

Consider using ActiveModelSerializers which allows you to define how your models should be serialized in a manner similar to ActiveRecord DSL (e.g. your problem would be solved by defining that event has_one :place)

samuil
  • 5,001
  • 1
  • 37
  • 44
0
    :events => events.as_json(include: :place)

This is a useful for my problem. After add belongs_to, obviously.

from http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html

Luis C.
  • 727
  • 3
  • 22
  • 34