[英]postgres_ext/serializers help… seems so simple, but can't get anything to work as described
https://github.com/dockyard/postgres_ext-serializers https://github.com/dockyard/postgres_ext-serializers
this seems so simple to setup, but I can't seem to get any basic functionality to work in terms of moving the JSON from rails to postgres. 这似乎很容易设置,但我似乎无法获得任何基本功能来将JSON从rails移动到postgres。 I tried including the following in my ams init, in my specific serializers and in my models, but it never seems to be activating.
我尝试在我的ams init,我的特定序列化程序和我的模型中包含以下内容,但它似乎永远不会激活。
I'm on Rails 4.2.3 and Ruby 2.2 我在Rails 4.2.3和Ruby 2.2上
and this is what I've tried adding to multiple files: 这就是我尝试添加到多个文件:
require 'postgres_ext/serializers'
需要'postgres_ext / serializers'
Really appreciate your help, I know I must be missing something obvious. 非常感谢你的帮助,我知道我必须遗漏一些明显的东西。
Update: To give a bit more context, it you read the README.md instructions for this gem, it just says" 更新:为了给出更多的上下文,你读了这个gem的README.md指令,它只是说“
Just require 'postgres_ext/serializers' and use ActiveModel::Serializers as you normally would!
只需要'postgres_ext / serializers'并像往常一样使用ActiveModel :: Serializers!
So I added require 'postgres_ext/serializers' to my application.rb, made a small edit to a serializer to see if it worked: 所以我在我的application.rb中添加了'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
Then I would run the following in my Rails console: 然后我将在我的Rails控制台中运行以下代码:
users = User.first(10)
ActiveModel::ArraySerializer.new(users, each_serializer: UserSerializer).to_json
But the __sql full name attribute was never shown and it didn't seem to pulling the data from postgres any differently than before. 但__sql全名属性从未显示过,它似乎没有从postgres中提取数据与以前有任何不同。
And this is what my application.rb looks like: 这就是我的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
Thanks! 谢谢!
Have you tried adding this require 'postgres_ext/serializers'
to the top of your config/application.rb
. 您是否尝试将此
require 'postgres_ext/serializers'
到config/application.rb
的顶部。 I guess this needs to be required before anything else, when it changes the Postgres drivers. 我猜这需要在其他任何事情之前,当它改变Postgres驱动程序时。
Update to answer your update: 更新以回答您的更新:
def full_name__sql
"first_name || ' ' || email"
end
should be: 应该:
def self.full_name__sql
"first_name || ' ' || email"
end
From the docs: 来自文档:
... call by looking for a class method with the same name and the suffix __sql ...
...通过查找具有相同名称和后缀__sql的类方法进行调用...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.