【发布时间】:2015-10-27 23:01:01
【问题描述】:
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
然后我会在我的 Rails 控制台中运行以下命令:
users = User.first(10)
ActiveModel::ArraySerializer.new(users, each_serializer: UserSerializer).to_json
但 __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
谢谢!
【问题讨论】:
-
请在您的问题中添加确切的错误消息。究竟什么是行不通的,你怎么知道的?
-
@EugZol 刚刚在我的帖子中添加了更多细节。感谢收看!
标签: ruby-on-rails ruby-on-rails-4 activemodel serialization