exists?
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-27)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (38)
- 4.1.8 (18)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (-7)
- 5.1.7 (0)
- 5.2.3 (32)
- 6.0.0 (0)
- 6.1.3.1 (1)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
exists?(conditions = :none)
public
Returns true if a record exists in the table that matches the id or conditions given, or false otherwise. The argument can take six forms:
-
Integer - Finds the record with this primary key.
-
String - Finds the record with a primary key corresponding to this string (such as '5').
-
Array - Finds the record that matches these find-style conditions (such as ['name LIKE ?', "%#{query}%"]).
-
Hash - Finds the record that matches these find-style conditions (such as {name: 'David'}).
-
false - Returns always false.
-
No args - Returns false if the relation is empty, true otherwise.
For more information about specifying conditions as a hash or array, see the Conditions section in the introduction to ActiveRecord::Base.
Note: You can’t pass in a condition as a string (like name = 'Jamie'), since it would be sanitized and then queried against the primary key column, like id = 'name = \'Jamie\''.
Person.exists?(5) Person.exists?('5') Person.exists?(['name LIKE ?', "%#{query}%"]) Person.exists?(id: [1, 4, 8]) Person.exists?(name: 'David') Person.exists?(false) Person.exists? Person.where(name: 'Spartacus', rating: 4).exists?