Skip to content

Instantly share code, notes, and snippets.

@swordray
Last active August 29, 2015 13:56
Show Gist options
  • Save swordray/9168494 to your computer and use it in GitHub Desktop.
Save swordray/9168494 to your computer and use it in GitHub Desktop.
ActiveRecord serialization with 1000% performance improvement by skipping instantiation
records = ActiveRecordClass.select().where().order().paginate()
# before
records.to_json
# after - with 1000% performance improvement
records.klass.connection.execute(records.to_sql).to_a.map { |record| [records.select_values, record].transpose.to_h } }.to_json
@itai-sagi
Copy link

how would you use this for active record relations?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment