Created
August 31, 2018 11:08
-
-
Save abhionlyone/086d94ad12b1258e459ab4a6e46339f1 to your computer and use it in GitHub Desktop.
Activerecord name starts with
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
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Overriding a module method from a gem in Rails
https://stackoverflow.com/questions/580314/overriding-a-module-method-from-a-gem-in-rails