Sha256: 5d166ca26a392d88ad25440425a159557e5cb55c2be08c55fda0a59c6bbde6f5
Contents?: true
Size: 816 Bytes
Versions: 3
Compression:
Stored size: 816 Bytes
Contents
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}" is_mysql = connection.class.name =~ /mysql/i target_type = is_mysql ? 'CHAR' : 'TEXT' # CHAR is only 1 character in PostgreSQL column_as_string = "CAST(#{qualified_column_name} AS #{target_type})" "#{column_as_string} = ?" end bindings = [identifier] * query_clauses.size where([query_clauses.join(' OR '), *bindings]).first end def self.find_by_anything!(identifier) find_by_anything(identifier) or raise ActiveRecord::RecordNotFound, "No column equals \"#{identifier}\"" end end
Version data entries
3 entries across 3 versions & 1 rubygems