Created
March 12, 2014 10:53
-
-
Save alexspeller/9504648 to your computer and use it in GitHub Desktop.
This file contains 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
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