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'
               }
          ]
        }
4

2 回答 2

2

考虑使用ActiveModelSerializers,它允许您定义模型应如何以类似于 ActiveRecord DSL 的方式进行序列化(例如,您的问题将通过定义该事件来解决has_one :place

于 2014-04-17T07:25:26.587 回答
0
    :events => events.as_json(include: :place)

这对我的问题很有用。添加后belongs_to,显然。

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

于 2014-04-25T17:40:38.060 回答