Created
February 1, 2016 00:54
-
-
Save BenMorganIO/55ab447962ed3bf5a8e7 to your computer and use it in GitHub Desktop.
JSON parsing in Postgres instead of Ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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