How to use ActiveModel :: Serializer with PostgreSQL JSON column

I am trying to use ActiveModel :: Serializer in conjunction with PostgreSQL database.

The problem I am facing is that whenever I include a type column json

in the serializer, I get:

SystemStackError (stack level too deep):
  actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:70

      

I don't want to do this as I need to access the data before returning it.

From schema.rb

:

create_table "jobs", force: true do |t|
    t.integer  "user_id"
    t.string   "tool"
    t.string   "name"
    t.json     "options"
    t.integer  "status"
    t.string   "version"
    t.datetime "created_at"
    t.datetime "updated_at"
end

      

job_serializer.rb

:

class JobSerializer < ApplicationSerializer
    attributes :id, :tool, :name, :status, :options, :version, :created_at

    has_many :inputs, serializer: FileLinkSerializer
end

      

Works fine if i remove :options

from attributes but fails when enabled as above.

+3


source to share


1 answer


The problem is there is a field named options

. I wrapped this box in a box titled tool_options

and everything worked fine.

class JobSerializer < ApplicationSerializer
  attributes :id, :tool, :name, :status, :tool_options, :version, :created_at
end

class Job < ActiveRecord::Base
  def tool_options
    options
  end
end

      



(Actually, I'll change my schema as it's not too late.)

+1


source







All Articles