Skip to content

Instantly share code, notes, and snippets.

@abhionlyone
Created August 31, 2018 11:08
Show Gist options
  • Save abhionlyone/086d94ad12b1258e459ab4a6e46339f1 to your computer and use it in GitHub Desktop.
Save abhionlyone/086d94ad12b1258e459ab4a6e46339f1 to your computer and use it in GitHub Desktop.
Activerecord name starts with
# initializers/active_record_initializers.rb
class ActiveRecord::Base
# do not accept a column_name from the outside without sanitizing it
# as this can be prone to sql injection
def self.starts_with(column_name, prefix)
where("lower(#{column_name}) like ?", "#{prefix.downcase}%")
end
end
# User.starts_with('name', 'ab').limit(1)
@abhionlyone
Copy link
Author

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