Skip to content

Instantly share code, notes, and snippets.

@BenMorganIO
Created February 1, 2016 00:54
Show Gist options
  • Select an option

  • Save BenMorganIO/55ab447962ed3bf5a8e7 to your computer and use it in GitHub Desktop.

Select an option

Save BenMorganIO/55ab447962ed3bf5a8e7 to your computer and use it in GitHub Desktop.
JSON parsing in Postgres instead of Ruby.
ActiveRecord::Base.class_eval do
class << self
delegate :row_to_json, to: :all
end
end
ActiveRecord::Relation.class_eval do
def row_to_json
'[' + row_to_json_result.values.join(',') + ']'
end
private
def row_to_json_result
klass.connection.execute(<<-SQL.squish)
SELECT row_to_json(t) FROM(#{to_sql}) t
SQL
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment