Topic: How to convert "true" to TrueClass

When creating a fixture for a cucumber test I came across a problem, my validations fail (as you can see below). The problem is that I have a boolean field called active that cucumber treats as a string when passing it to Cucumber::Ast::Table. Does anyone know a work around? I found the <a href="">this</a> post on this problem but unfortunately the author only deals with finding a record, not creating one. Any solution would be appreciated.


Feature: Viewing the Portfolio
  As one who works for a living
  I want to record to the projects that I work on
  So that the world can see how good I really am!
    Given the following projects exist:
      | name       | url                | active |
      | Project 1  |  | true   |
      | Project 2  |  | false  |
      | Project 3  |  | true   |
      | Project 4  |  | true   |
      | Project 5  |  | true   |
      | Project 6  |  | false  |
      | Project 7  |  | true   |
      | Project 8  |  | true   |
      | Project 9  |  | false  |
      | Project 10 | | false  |
      | Project 11 | | true   |
      Validation failed: Active can't be blank (ActiveRecord::RecordInvalid)
      ./features/step_definitions/project_steps.rb:5:in `each'
      ./features/step_definitions/project_steps.rb:5:in `/^the following projects exist:$/'
      features/portfolio.feature:7:in `Given the following projects exist:'

Re: How to convert "true" to TrueClass

When setting a boolean attribute in an ActiveRecord model, Rails will convert certain strings like "true", "TRUE" or "1" into a boolean true. That's why I think your problem is caused by something different, like active not being an accessible attribute for mass assignment.

Re: How to convert "true" to TrueClass

I thought the same... So I removed all calls to attr_accessible... Schema and model look like:

ActiveRecord::Schema.define(:version => 20100503110501) do
  create_table "projects", :force => true do |t|
    t.string   "name",                     :limit => 64,                    :null => false
    t.string   "slug",                                                      :null => false
    t.boolean  "active",                                  :default => true, :null => false
    t.string   "url",                      :limit => 128
    t.datetime "completed_at"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "screen_grab_file_name"
    t.string   "screen_grab_content_type"
    t.integer  "screen_grab_file_size"
    t.datetime "screen_grab_updated_at"

class Project < ActiveRecord::Base
  validates_presence_of :name, :slug, :active
  validates_inclusion_of :active, :in => [true, false]
  before_validation :create_slug, :on => :create

    def create_slug
      self.slug = name.parameterize

Do you have any other ideas? Also I have tried changing true / false to t / f and 1 / 0 and still no luck.

Re: How to convert "true" to TrueClass

validates_presence_of means "validate it is true" when applied to boolean attributes.

Re: How to convert "true" to TrueClass

Right. Thanks for clearing that up. All green now smile