【问题标题】:How to use ActiveModel::Serializer with a PostgreSQL JSON column如何将 ActiveModel::Serializer 与 PostgreSQL JSON 列一起使用
【发布时间】:2014-09-15 14:25:38
【问题描述】:

我正在尝试将ActiveModel::Serializer 与 PostgreSQL 数据库结合使用。

我遇到的问题是,每当我在序列化程序中包含 json 类型列时,我都会得到:

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

我不想做this,因为我需要在返回数据之前访问它。

来自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

如果我从属性中删除 :options 可以正常工作,但在如上包含它时会崩溃。

【问题讨论】:

  • 我只是想知道序列化程序在 json 列中的表现如何,进行了快速搜索,然后我们开始了 :) 谢谢。

标签: ruby-on-rails json postgresql active-model-serializers


【解决方案1】:

问题在于有一个名为options 的字段。我将该字段包装在一个名为 tool_options 的字段中,一切正常。

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

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

(实际上我会改变我的架构,因为现在还为时不晚。)

【讨论】:

  • 刚刚在 active_model_serializers 0.8 中遇到了同样的问题。它不会在 0.9 中发生,因为冲突选项功能似乎已被删除。
猜你喜欢
  • 1970-01-01
  • 2015-08-05
  • 2018-04-28
  • 2014-10-28
  • 2021-10-09
  • 1970-01-01
  • 2019-02-19
  • 1970-01-01
  • 2014-01-15
相关资源
最近更新 更多