我正在尝试向我的用户模型添加验证,但数据没有失败,而是继续传递。对于名字,我没有条目。见下文:
1.9.3p286 :012 > user = User.new(name: "", email: "fail@fail.com")
=> #<User id: nil, name: "", email: "fail@fail.com", created_at: nil, updated_at: nil>
1.9.3p286 :013 > user.save
(0.1ms) SAVEPOINT active_record_1
SQL (1.5ms) INSERT INTO "users" ("created_at", "email", "name", "updated_at") VALUES (?, ?, ?, ?) [["created_at", Sun, 23 Dec 2012 18:52:00 UTC +00:00], ["email", "fail@fail.com"], ["name", ""], ["updated_at", Sun, 23 Dec 2012 18:52:00 UTC +00:00]]
(0.1ms) RELEASE SAVEPOINT active_record_1
=> true
这是我所拥有的app/models/user.rb
:
class User < ActiveRecord::Base
attr_accessible(:name, :email)
validates(:name, presence: true)
end
这应该是失败的。有什么建议么?谢谢!