Question

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'
               }
          ]
        }
Was it helpful?

Solution 2

    :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

OTHER TIPS

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top