Read more

Virtus: Coercing boolean attributes

Henning Koch
June 06, 2013Software engineer at makandra GmbH

TLDR

Do it like this:

attribute :active, Virtus::Attribute::Boolean

Long story

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

In Virtus Show archive.org snapshot you define attribute with their type like this:

attribute :name, String
attribute :birthday, Date

When defining a boolean attributes, you will probably write it like this:

attribute :active, Boolean

The problem is, there is not actually a Boolean class in Ruby (there's only TrueClass and FalseClass), so use Virtus::Attribute::Boolean instead.

The reason while Boolean sometimes survives compilation is that many libraries define a global Boolean class for some reason. That class however won't work with virtus.

Posted by Henning Koch to makandra dev (2013-06-06 18:23)