Quick Tip: Knowing if an Object’s Attribute is Empty or Nil

by Charles Max Wood on June 18, 2009

I was browsing the ActiveRecord documentation and I came across notation that looks like this:

User.name?

When I looked more closely, I realized that it replaced all of the places where I had something like this:

!User.name.blank?

In other words, if the user’s name is an empty string (“”) or nil, I can call User.name? and it’ll return false. Here’s the documentation I was browsing. http://api.rubyonrails.org/classes/ActiveRecord/Base.html

Previous post:

Next post: