I.e., is
Post.title?
equivalent to
Post.title.present?
I.e., is
Post.title?
equivalent to
Post.title.present?
No.
Object#present?
is the same thing as calling !obj.blank?
.
The "attribute?" method might end up calling the same code, but it might not, and it depends on the column type that you're dealing with.
The easiest way to see these not return the same value is to access a numeric column. Say you had foo.score
as a decimal column in your db, and you set it to zero. You'd see the following behavior.
foo.score = 0
foo.score? # false
foo.score.present? # true
The code for the "?" method is in ActiveRecord::AttributeMethods.
def query_attribute(attr_name)
unless value = read_attribute(attr_name)
false
else
column = self.class.columns_hash[attr_name]
if column.nil?
if Numeric === value || value !~ /[^0-9]/
!value.to_i.zero?
else
return false if ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES.include?(value)
!value.blank?
end
elsif column.number?
!value.zero?
else
!value.blank?
end
end
end