我正在尝试在Ruby on Rails中为User模型编写单元测试。我正在使用authlogic,需要检查用户注册时用户模型属性的first_name和last_name是不一样的。
这是我的用户模型:
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.login_field= :username
end
has_many :memberships, :class_name => "Project::Membership"
has_many :projects, :through => :memberships
has_one :profile
validates :email, :presence => true, :uniqueness => true
validates :username, :presence => true, :uniqueness => true
validates :first_name,:presence => true
validates:last_name, :presence => true
validates :title, :presence => true
validates :password, :presence => true
validates :password_confirmation, :presence => true
validates :gender, :presence => true
# Custom validator
validates :first_name, :last_name, :different_names => true
正如您所看到的,我尝试创建一个自定义验证器,在/lib/different_names_validator.rb中使用名为DifferntNamesValidator的类创建一个新文件,但无法获取它,因为我收到以下错误:Unknown validator:' different_names'(ArgumentError)
提前致谢!
答案 0 :(得分:1)
您好试着在模型中包含此模块