ruby-on-railsgrape-api

Ruby on Rails Grape API omitting attributes


I'm building a grape API in RoR, and I'm trying to make a GET end-point for my model, but the user_id, created_at and updated_at attributes get cut off from the actual JSON object.

This is my end-point:

resource :pairings do
  desc 'Gets all pairings from database'
  get do
    Pairing.all
  end
end

The Pairing model looks like this:

create_table "pairings", force: :cascade do |t|
  t.string "dumb_unit_id", null: false
  t.string "smart_unit_id", null: false
  t.datetime "created_at", precision: 6, null: false
  t.datetime "updated_at", precision: 6, null: false
  t.integer "user_id", null: false
  t.index ["dumb_unit_id"], name: "index_pairings_on_dumb_unit_id", unique: true
  t.index ["smart_unit_id"], name: "index_pairings_on_smart_unit_id", unique: true
end

The pairing ends up looking like this in the response from the API:

{"id"=>979, "dumb_unit_id"=>"wu5qg63k6c1iz", "smart_unit_id"=>"rjnc8yg53j29c"}

No user_id or timestamps.


Solution

  • I found the problem. I had an auto-generated serializer that only defined :id, :dumb_unit_id and :smart_unit_id as attributes. Adding :user_id as an attribute in this file solved the issue.