# NoMethodError when trying to validate model field

**URL:** <https://www.ruby-forum.com/t/nomethoderror-when-trying-to-validate-model-field/127414>\
**Category:** Ruby\
**Created:** [January 29, 2008, 12:41am UTC](https://www.ruby-forum.com/t/nomethoderror-when-trying-to-validate-model-field/127414 "2008-01-29T00:41:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [January 29, 2008, 12:41am UTC](https://www.ruby-forum.com/t/nomethoderror-when-trying-to-validate-model-field/127414/1 "2008-01-29T00:41:21Z")

</div>

Hi, When validating a field in my user model, I wrote a method  
“is\_client?” to help with the validation. But I did it wrong because  
now I’m getting this error:

NoMethodError in SessionController#create  
undefined method `is_client?' for User:Class RAILS_ROOT: ./script/../config/.. Application Trace | Framework Trace | Full Trace /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/base.rb:1238:in`method\_missing’  
/usr/local/apache2/htdocs/easyrx/app/models/user.rb:16  
/usr/local/apache2/htdocs/easyrx/app/controllers/session\_controller.rb:  
12:in `create’

Here’s the relevant code. Any ideas what I can do to get rid of this  
error? Thanks, - Dave

class User \< ActiveRecord::Base  
…  
validates\_as\_phone :work\_phone, :if =\> !:work\_phone.blank? &&  
is\_client?, :message =\> “is not a valid phone number” # line  
16

attr\_accessible :user\_type\_id, :user\_group\_id, :pharmacy\_id, :phone,  
:work\_phone, :work\_phone\_extension, :fax, :bill\_to\_first\_name,  
:bill\_to\_last\_name, :bill\_to\_street, :bill\_to\_city, :bill\_to\_state,  
:bill\_to\_zip, :bill\_to\_country, :ship\_to\_first\_name, :ship\_to\_last\_name,  
:ship\_to\_street, :ship\_to\_city, :ship\_to\_state, :ship\_to\_zip,  
:ship\_to\_country, :bill\_to\_attn, :ship\_to\_attn, :bill\_to\_company,  
:ship\_to\_company, :bill\_to\_street2, :ship\_to\_street2, :login, :email,  
:password, :password\_confirmation

…  
def is\_client?  
user\_type\_id == 2  
end

end

---

<div class="post-metadata">

**Author:** ![laredotornado](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9dc877/32.png) [@laredotornado](https://www.ruby-forum.com/u/laredotornado)\
**Post date:** [January 29, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-trying-to-validate-model-field/127414/2 "2008-01-29T22:15:35Z")

</div>

I can explain why you’re getting this error. validates\_as\_phone is a  
method. All methods in Ruby have a receiver object. Because  
validates\_as\_phone is called outside of a defined method, it’s  
receiver object is the User class itself, _not_ an object of type  
User. The arguments that you pass to the validates\_as\_phone method  
include a call to the is\_client? method. Since the receiver object is  
the User class, it looks for a method called is\_client? defined for  
the User class, which would look like this:

def User.is\_client?  
user\_type\_id == 2  
end

or another way to say the same thing is:

def self.is\_client?  
user\_type\_id == 2  
end

Instead, it finds a method defined for User objects:

def is\_client?  
user\_type\_id == 2  
end

Unfortunately, you can’t just stick a self. onto your is\_client?  
method because user\_type\_id is a method that belongs to User objects,  
not the User class itself.
