Skip to content

Instantly share code, notes, and snippets.

@alexspeller
Created March 12, 2014 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexspeller/9504648 to your computer and use it in GitHub Desktop.
Save alexspeller/9504648 to your computer and use it in GitHub Desktop.
def index
# use whatever AR scopes you want to make your sql query
relation = Post.find(1).comments.recent.where(author: params[:author_id])
# Then convert to sql to get an array of arrays back - way faster than
# full-on ActiveRecord objects
result = ActiveRecord::Base.connection.execute(relation.to_sql).map do |row|
{
author_id: row[0],
something: row[1],
something_else: row[2]
}
end
# yajl is a fast JSON serializer written in C
render json: Yajl::Encoder.encode(result)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment