为什么我的DataMapper验证不起作用?

时间:2010-11-05 06:33:29

标签: ruby datamapper validation

此示例不会为我验证标题的存在:

require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-validations'

db_name = 'rvtest.sqlite'

DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite://' + Dir.pwd + '/' + db_name)

class Task
  include DataMapper::Resource
  property :id,         Serial
  property :title,      String

  validates_presence_of :title
end

DataMapper.finalize
DataMapper.auto_upgrade!

然而,这个确实:

require 'rubygems'
require 'dm-core'
require 'dm-migrations'
require 'dm-validations'

db_name = 'rvtest.sqlite'

DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, 'sqlite://' + Dir.pwd + '/' + db_name)

class Task
  include DataMapper::Resource
  property :id,         Serial
  property :title,      String, :required => true

end

DataMapper.finalize
DataMapper.auto_upgrade!

为什么这些工作方式不同?如果我进入irb,请求此文件并输入Task.new.save!,第一个返回true,第二个返回false

1 个答案:

答案 0 :(得分:2)

保存!不会调用任何回调,这意味着它不会调用验证。尝试保存