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. I tried including the following in my ams init, in my specific serializers and in my models, but it never seems to be activating.
I'm on Rails 4.2.3 and Ruby 2.2
and this is what I've tried adding to multiple files:
require '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"
Just require 'postgres_ext/serializers' and use ActiveModel::Serializers as you normally would!
So I added require 'postgres_ext/serializers' to my application.rb, made a small edit to a serializer to see if it worked:
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:
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.
And this is what my application.rb looks like:
# 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
. I guess this needs to be required before anything else, when it changes the Postgres drivers.
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With