我试图添加验证到我的用户模型,但不是失败,数据一直通过。至于名字,我没有条目。见下文:
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
这应该会失败。有什么建议吗?谢谢你!
它应该工作,我认为你没有在添加验证后重新加载控制台