Skip to content

Instantly share code, notes, and snippets.

@kohgpat
Created October 4, 2012 07:00
Show Gist options
  • Select an option

  • Save kohgpat/3831916 to your computer and use it in GitHub Desktop.

Select an option

Save kohgpat/3831916 to your computer and use it in GitHub Desktop.
ActiveRecord, Arel and Oj
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