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="http://www.claytonlz.com/index.php/2009/06/using-rubys-true-in-cucumber-multiline-tables/">this</a> post on this problem but unfortunately the author only deals with finding a record, not creating one. Any solution would be appreciated.

Thanks.

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!
  
  Background:
    Given the following projects exist:
      | name       | url                | active |
      | Project 1  | www.project-1.com  | true   |
      | Project 2  | www.project-2.com  | false  |
      | Project 3  | www.project-3.com  | true   |
      | Project 4  | www.project-4.com  | true   |
      | Project 5  | www.project-5.com  | true   |
      | Project 6  | www.project-6.com  | false  |
      | Project 7  | www.project-7.com  | true   |
      | Project 8  | www.project-8.com  | true   |
      | Project 9  | www.project-9.com  | false  |
      | Project 10 | www.project-10.com | false  |
      | Project 11 | www.project-12.com | true   |
      Validation failed: Active can't be blank (ActiveRecord::RecordInvalid)
      ./features/step_definitions/project_steps.rb:5
      ./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"
  end
end

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

  private
  
    def create_slug
      self.slug = name.parameterize
    end
end

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

Thanks