ActiveRecord: Query Attributes
You can use
attribute?as shorthanded version of
attribute.present?, except for numeric attributes and associations.
attribute? is generated for all attributes and not only for boolean attributes.
These methods are using
#query_attribute under the hood. For more details you can see
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
contract.savings = BigDecimal('0.0') contract.savings? # => false contract.query_attribute(:savings) # => false contract.savings.present? # => true
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.
ActiveType you can treat
attribute? as a real shorthanded version of
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
attribute? methods are not generated for associations.
query_attributeon associations. In case of Rails 7 you are getting an error. In Rails 6 this method is always responding with
Your development team has a full backlog of feature requests, chores and refactoring coupled with deadlines? We are familiar with that. With our "DevOps as a Service" offering, we support developer teams with infrastructure and operations expertise.