Last active
December 20, 2015 04:39
-
-
Save Fivell/6072447 to your computer and use it in GitHub Desktop.
find by anything activerecord
This file contains hidden or 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
ActiveRecord::Base.class_eval do | |
def self.find_by_anything(identifier) | |
matchable_columns = columns.reject { |column| [:binary, :boolean].include?(column.type) } | |
query_clauses = matchable_columns.collect do |column| | |
qualified_column_name = "#{table_name}.#{column.name}" | |
column_as_string = "CAST(#{qualified_column_name} AS CHAR)" | |
"#{column_as_string} = ?" | |
end | |
bindings = [identifier] * query_clauses.size | |
find(:first, :conditions => [query_clauses.join(' OR '), *bindings]) | |
end | |
def self.find_by_anything!(identifier) | |
find_by_anything(identifier) or raise ActiveRecord::RecordNotFound, "No column equals \"#{identifier}\"" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment