method

find_by

Importance_0
v6.0.0 - 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 182
      def find_by(*args) # :nodoc:
        return super if scope_attributes? || reflect_on_all_aggregations.any? ||
                        columns_hash.key?(inheritance_column) && !base_class?

        hash = args.first

        return super if !(Hash === hash) || hash.values.any? { |v|
          StatementCache.unsupported_value?(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, connection)&.first
        rescue TypeError
          raise ActiveRecord::StatementInvalid
        end
      end
Register or log in to add new notes.