开发者

Validating content of the attributes to not be the same

开发者 https://www.devze.com 2023-01-19 23:45 出处:网络
I am trying to write a unit testing for a User model in Ruby on Rails. I am using authlogic and need to check that the first_name and last_name of the user model attributes are not the same when the u

I am trying to write a unit testing for a User model in Ruby on Rails. I am using authlogic and need to check that the first_name and last_name of the user model attributes are not the same when the user is registering.

This is my user model:

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

As you can see, I tried to create a custom validator creating a new file in /lib/different_names_validator.rb with a class called DifferntNamesValidator, but couldn't开发者_JAVA技巧 get it, as I got the following error: Unknown validator: 'different_names' (ArgumentError)

Thanks in advance!


Hi Try to include this module in your model

0

精彩评论

暂无评论...
验证码 换一张
取 消