Skip to content

Instantly share code, notes, and snippets.

@localhostdotdev
Last active April 14, 2019 09:17
Show Gist options
  • Select an option

  • Save localhostdotdev/22e721f7597d0956b7da313daad08616 to your computer and use it in GitHub Desktop.

Select an option

Save localhostdotdev/22e721f7597d0956b7da313daad08616 to your computer and use it in GitHub Desktop.
ApplicationRecord.sql('select * from users left join accounts on accounts.user_id = users.id and accounts.kind = ?', 'something')
class ApplicationRecord < ActiveRecord::Base
def self.sql(sql, *values)
ActiveRecord::Base.connection.execute(
ActiveRecord::Base.send(:sanitize_sql_array, [sql, *values])
).each.to_a
end
end
users = ApplicationRecord.sql('select * from users')
users_and_accounts = ApplicationRecord.sql('select * from users left join accounts on accounts.user_id = users.id and accounts.kind = ?', 'something')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment