before_save not working with Rails 3
- by Mich Dart
I have this Project model:
class Project < ActiveRecord::Base
validates :status, :inclusion => { :in => ['active', 'closed'] }
validates :title,
:presence => true,
:length => { :in => 4..30 }
before_save :set_default_status_if_not_specified
private
def set_default_status_if_not_specified
self.status = 'active' if self.status.blank?
end
end
If I create a new object like this:
Project.create!(:title => 'Test 2', :pm_id => 1)
I get these errors: Validation failed: Status is not included in the list
But status field should get filled in before save.