Created
October 4, 2012 07:00
-
-
Save kohgpat/3831916 to your computer and use it in GitHub Desktop.
ActiveRecord, Arel and Oj
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
| require 'activerecord' | |
| require 'oj' | |
| require 'json' | |
| # Some ActiveRecord inherited class | |
| class SomeARClass < ActiveRecord::Base | |
| # Find a record from the database as hash without AR object initialization | |
| def self.lightning id | |
| # Connection = Database Adapter = Mysql2Adapter | |
| connection.select(where(:id => id).to_sql).each do |attrs| | |
| attrs.each do |attr| | |
| attrs[attr] = type_cast_attribute(attr, attrs) | |
| end | |
| end[0] | |
| end | |
| end | |
| json = Oj.dump SomeArClass.lightning(1), :mode => :compat | |
| JSON.parse json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment