Forum: Ruby on Rails validates_presence_of

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2ec99eb26cccaf2a43500038de4396f3?d=identicon&s=25 rhywun (Guest)
on 2006-12-29 01:10
(Received via mailing list)
I'm confused about the usage of 'validates_presence_of' and the
'default' parameter of the 'column' method. Let's say I have a model
that looks something like this:

  class CreateErrorTypes < ActiveRecord::Migration
    def self.up
      create_table :error_types do |t|
        t.column :description, :string, :limit => 254, :null => false
        t.column :can_delete, :boolean, :null => false, :default =>
false
      end
    end
    def self.down
      drop_table :error_types
    end
  end

  class ErrorType < ActiveRecord::Base
    validates_presence_of :description
    validates_presence_of :can_delete
  end

Then in console I do this:

  >> ErrorType.create(:description => "General error")

What I expect is an ErrorType object with can_delete set to false, but
what I get is an error saying that can_delete "can't be blank". Why
isn't it setting the default value of that field to false??
This topic is locked and can not be replied to.