Read more

ActiveRecord: Query Attributes

Julian
March 21, 2022Software engineer at makandra GmbH

tl;dr
You can use attribute? as shorthanded version of attribute.present?, except for numeric attributes and associations.

Technical Details

attribute? is generated for all attributes and not only for boolean attributes.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

These methods are using #query_attribute under the hood. For more details you can see ActiveRecord::AttributeMethods::Query.

In most circumstances query_attribute is working like attribute.present?. If your attribute is responding to :zero? then you have to be aware that query_attribute is responding like !zero?.

contract.savings = BigDecimal('0.0')

contract.savings?
# => false

contract.query_attribute(:savings)
# => false

contract.savings.present?
# => true

ActiveType

Virtual attributes of ActiveType are not using query_attribute. If you are using attribute? on a virtual attribute then it's using present? under the hood.

So for ActiveType you can treat attribute? as a real shorthanded version of attribute.present?.

class Human::Form < ActiveType::Record[Human]
  attribute: number_of_teeth
end

human.age = 0
human.number_of_teeth = 0

human.age?
# => false

human.number_of_teeth?
# => true

Associations

attribute? methods are not generated for associations.

Warning

Don't use query_attribute on associations. In case of Rails 7 you are getting an error. In Rails 6 this method is always responding with false.

Posted by Julian to makandra dev (2022-03-21 16:49)