Does a column of a time column mean an answer to calls to logical methods?

I just noticed that I can call boolean methods on AR timestamps. Can I depend on this behavior, or is it a failure?

foo.updated_at? #=> true if not nil, but false if nil

This is useful if I have a column @task.completed_on. I can use completed_onto determine if a task has completed (has a timestamp or is zero).

@task.completed?
class Task < ActiveRecord::Base
  def completed?
    completed_on?
  end
end
+3
source share
1 answer

Rails provides predicate methods (methods that return a boolean and are marked with a question mark) for all attributes in the ActiveRecord model, regardless of the type of column in the database, returns true if it has a value or false if it is zero. This is not a glitch.

, , :

u = User.find_by(email: "foo@bar.com") 
u.email? # => true

u = User.new(email: "new@user.com")
u.email? # => true

. , completed?.

+1

All Articles