method

find_by

Importance_0
v5.1.7 - Show latest stable - 0 notes - Class: ClassMethods
find_by(*args) public

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/core.rb, line 198
      def find_by(*args) # :nodoc:
        return super if scope_attributes? || reflect_on_all_aggregations.any?

        hash = args.first

        return super if !(Hash === hash) || hash.values.any? { |v|
          v.nil? || Array === v || Hash === v || Relation === v || Base === v
        }

        # We can't cache Post.find_by(author: david) ...yet
        return super unless hash.keys.all? { |k| columns_hash.has_key?(k.to_s) }

        keys = hash.keys

        statement = cached_find_by_statement(keys) { |params|
          wheres = keys.each_with_object({}) { |param, o|
            o[param] = params.bind
          }
          where(wheres).limit(1)
        }
        begin
          statement.execute(hash.values, self, connection).first
        rescue TypeError
          raise ActiveRecord::StatementInvalid
        rescue ::RangeError
          nil
        end
      end
Register or log in to add new notes.