简体   繁体   English

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

[英]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.

相关问题 修补ActiveRecord / postgres_ext以返回大写的UUID - Patch ActiveRecord/postgres_ext to return upper case UUID 在postgis适配器中使用postgres_ext不适用于数组列 - Using postgres_ext with postgis adapter not working with array columns Rails 3.2模式转储使用postgres_ext gem将所有UUID列转换为text和mangles数组声明 - Rails 3.2 schema dump turns all UUID columns to text and mangles array declarations with postgres_ext gem 计算Active Admin仪表板(Rails,Active admin 1.0,Postgresql数据库,postgres_ext gem)中序列化属性(数组)中值的出现 - Count occurrence of values in a serialized attribute(array) in Active Admin dashboard (Rails, Active admin 1.0, Postgresql database, postgres_ext gem) 尝试在Active Admin table_for(Rails,Postgresql,postgres_ext gem)上使用数组数据时,[xxx]的错误未定义方法`to_key&#39;:Array: - Error undefined method `to_key' for [xxx] :Array when trying to use array data on Active Admin table_for (Rails, Postgresql, postgres_ext gem) 无法使Dropdown Association与simple_form一起使用 - Can't get Dropdown Association to work with simple_form 简单的link_to_remote函数-无法正常工作 - Simple link_to_remote function - can't get it to work 无法使这个简单的Google自动完成脚本起作用(Rails) - Can't get this simple Google Autocomplete script to work (Rails) 可以将行锁定以使其无法读取(使用Rails / Postgres)吗? - Can a row be locked so it can't be read [using Rails/Postgres]? 错误未定义的方法“管理员?” 为nil:NilClass-似乎简单将无法正常工作 - Error Undefined method `admin?' for nil:NilClass - Seems simple won't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM