Skip to content

Instantly share code, notes, and snippets.

@amcclosky
Created January 9, 2012 21:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amcclosky/1585057 to your computer and use it in GitHub Desktop.
Save amcclosky/1585057 to your computer and use it in GitHub Desktop.
Simple Rails lib for executing raw SQL
module RawSqlUtil
def sql_select(sql, params)
# We have to do this weirdness because sanitize_sql_array is a protected method
query = ActiveRecord::Base.send(:sanitize_sql_array, [sql].concat(params))
ActiveRecord::Base.connection.select_all(query)
end
def sql_select_values(sql, params, result_cols)
# Returns an array of single values if only one result_col is given, otherwise it returns an array of arrays whose
# values correspond to the given result_cols order.
result = sql_select(sql, params)
if not result_cols.is_a?(Array)
# return single values
result.map { |row| row[result_cols] } || []
else
result.map do |row|
# return array of values
result_cols.each { |col| row[col] }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment