Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Update ActiveRecord::AttributeMethods#attribute_present? to return false for empty strings #5316

merged 1 commit into from

3 participants


Right now, attribute_present? returns true on empty strings, when the documentation says that it should return false:

"Returns true if the specified +attribute+ has been set by the user or by a database load and is neither
nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings)."

This change fixes the function to correctly return false for empty strings. Test is included.

Fixes #5314


Thanks for the report. We should just use value.present? in the implementation, no? In any case, why would someone use such method? We have better approaches today like or even user.attribute?(:name) which does basically the same thing.


I agree with you that this method (which as far as I can tell is super old) is ripe for deprecation.

The problem with using value.present? is that false.present? is false, which leads to the odd behavior (and previously documented bug) where you set an attribute to false, and then attribute_present? returns false. See #1613.


Yeah. This fix is fine to me. Using value.present? will not work like expected.

Another thing, and user.attribute?(:name) do not work in the same way that user.attribute_present?(:name).

@josevalim josevalim merged commit 3da31b9 into rails:master

Should I backport this to 3-2-stable?


Yes. It is a regression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
2  activerecord/lib/active_record/attribute_methods.rb
@@ -189,7 +189,7 @@ def attribute_for_inspect(attr_name)
# nil nor empty? (the latter only applies to objects that respond to empty?, most notably Strings).
def attribute_present?(attribute)
value = read_attribute(attribute)
- !value.nil? || (value.respond_to?(:empty?) && !value.empty?)
+ !value.nil? && !(value.respond_to?(:empty?) && value.empty?)
# Returns the column object for the named attribute.
3  activerecord/test/cases/attribute_methods_test.rb
@@ -30,9 +30,12 @@ def test_attribute_present
t =
t.title = "hello there!"
t.written_on =
+ t.author_name = ""
assert t.attribute_present?("title")
assert t.attribute_present?("written_on")
assert !t.attribute_present?("content")
+ assert !t.attribute_present?("author_name")
def test_attribute_present_with_booleans
Something went wrong with that request. Please try again.