1

Let's say you have a model called Widget which includes :id and :name.

In your Widget#index endpoint, you want to render all widgets in json, however... you also want to include another value for each record that isn't apart of the model called foobar. So you want the end result to look like this...

{
    "widgets": [
        {
            "id":, 1,
            "name": "widgy",
            "foobar": true
        },
        {
            "id":, 2,
            "name": "gadgy",
            "foobar": false
        }
    ]
}

How can you edit the following code to allow for something like this?

widgets = Widget.all
render json: widgets
chris P
  • 6,359
  • 11
  • 40
  • 84

1 Answers1

0

Depending on the version of Rails you're running, you could choose to use active_model_serializers or a simple jbuilder view.

Rails 4 already includes the jbuilder gem, so you don't need to include anything special in your Gemfile.

Cheers!

  • I'm using AMS in my app. I don't have the built in jbuilder as I'm on Rails 3. Do you know if there is a simple way to do this just through code? If not, how can I access it from my AMS serializer. The problem is, I don't have the user_id param in my Walkthru serializer that I would need to access table WalkthruComplete. – chris P Jul 27 '15 at 23:36
  • This SO post details passing arguments to your serializer: http://stackoverflow.com/questions/23347587/how-to-pass-parameters-to-activemodel-serializer – chris P Jul 28 '15 at 17:38