# NoMethodError when trying to validate model field

**URL:** <https://rubytalk.org/t/nomethoderror-when-trying-to-validate-model-field/44098>\
**Category:** ruby-talk\
**Created:** [28 January 2008 23:40 UTC](https://rubytalk.org/t/nomethoderror-when-trying-to-validate-model-field/44098 "2008-01-28T23:40:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![laredotornado1](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@laredotornado1](https://rubytalk.org/u/laredotornado1)\
**Post date:** [28 January 2008 23:40 UTC](https://rubytalk.org/t/nomethoderror-when-trying-to-validate-model-field/44098/1 "2008-01-28T23:40:00Z")

</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  
&nbsp;&nbsp;...  
&nbsp;&nbsp;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

...  
&nbsp;&nbsp;&nbsp;&nbsp;def is\_client?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user\_type\_id == 2  
&nbsp;&nbsp;&nbsp;&nbsp;end

end

---

<div class="post-metadata">

**Author:** ![Wyatt\_Greene](https://avatars.discourse-cdn.com/v4/letter/w/82dd89/32.png) [@Wyatt\_Greene](https://rubytalk.org/u/Wyatt_Greene)\
**Post date:** [29 January 2008 21:14 UTC](https://rubytalk.org/t/nomethoderror-when-trying-to-validate-model-field/44098/2 "2008-01-29T21:14:56Z")

</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:

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

or another way to say the same thing is:

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

Instead, it finds a method defined for User objects:

&nbsp;&nbsp;def is\_client?  
&nbsp;&nbsp;&nbsp;&nbsp;user\_type\_id == 2  
&nbsp;&nbsp;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.

> **···**
>
> On Jan 28, 6:37 pm, laredotornado \<laredotorn...@zipmail.com\> wrote:
> 
> > 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  
> > &nbsp;&nbsp;...  
> > &nbsp;&nbsp;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
> > 
> > ...  
> > &nbsp;&nbsp;&nbsp;&nbsp;def is\_client?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user\_type\_id == 2  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > end
