postgres_ext/serializers help ...看起来很简单,但无法按照描述获得任何工作

avi*_*ian 5 ruby-on-rails serializer activemodel ruby-on-rails-4

https://github.com/dockyard/postgres_ext-serializers

这似乎很容易设置,但我似乎无法获得任何基本功能来将JSON从rails移动到postgres.我尝试在我的ams init,我的特定序列化程序和我的模型中包含以下内容,但它似乎永远不会激活.

我在Rails 4.2.3和Ruby 2.2上

这就是我尝试添加到多个文件:

需要'postgres_ext/serializers'

非常感谢你的帮助,我知道我必须遗漏一些明显的东西.

更新:为了给出更多的上下文,你读了这个gem的README.md指令,它只是说"

只需要'postgres_ext/serializers'并像往常一样使用ActiveModel :: Serializers!

所以我在我的application.rb中添加了require'postgres_ext/serializers',对序列化器进行了一个小编辑,看它是否有效:

class UserSerializer < ActiveModel::Serializer

  cached false

  attributes :id, :username, :location, :full_name

  def full_name
    "#{object.first_name} #{object.last_name}"
  end

  def full_name__sql
    "first_name || ' ' || email"
  end

end
Run Code Online (Sandbox Code Playgroud)

然后我将在我的Rails控制台中运行以下代码:

users = User.first(10)
ActiveModel::ArraySerializer.new(users, each_serializer: UserSerializer).to_json
Run Code Online (Sandbox Code Playgroud)

但__sql全名属性从未显示过,它似乎没有从postgres中提取数据与以前有任何不同.

这就是我的application.rb看起来像:

# require 'postgres_ext/serializers' ### Doesn't work here

require File.expand_path('../boot', __FILE__)

require 'rails/all'

require 'postgres_ext/serializers'

Bundler.require(*Rails.groups)

module Baller
  class Application < Rails::Application
    # Do not swallow errors in after_commit/after_rollback callbacks.
    config.active_record.raise_in_transactional_callbacks = true

    # From: http://www.benfranklinlabs.com/where-to-put-rails-modules/
    # config.autoload_paths += %W(#{config.root}/lib) # add this line
    config.autoload_paths += %W(#{config.root}/lib)
    config.autoload_paths += Dir["#{config.root}/lib/**/"]
  end
end
Run Code Online (Sandbox Code Playgroud)

谢谢!

sma*_*ton 2

您是否尝试过将其添加require 'postgres_ext/serializers'到您的config/application.rb. 我想当它改变 Postgres 驱动程序时,这需要先于其他事情。

更新以回答您的更新:

def full_name__sql
  "first_name || ' ' || email"
end
Run Code Online (Sandbox Code Playgroud)

应该:

def self.full_name__sql
 "first_name || ' ' || email"
end
Run Code Online (Sandbox Code Playgroud)

来自文档:

...通过查找具有相同名称和后缀 __sql 的类方法来调用...