# Model Methods

**URL:** <https://www.ruby-forum.com/t/model-methods/157599>\
**Category:** Rails\
**Created:** [January 13, 2009, 3:37pm UTC](https://www.ruby-forum.com/t/model-methods/157599 "2009-01-13T15:37:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [January 13, 2009, 3:37pm UTC](https://www.ruby-forum.com/t/model-methods/157599/1 "2009-01-13T15:37:46Z")

</div>

I would like to write a few custom methods for my model, but keep  
getting an error saying the method is not defined for the model (playing  
in the console).

For example, I have a product model, and I want a method called stocked?  
that will return ‘yes’ or ‘no’ depending on an in\_stock boolean field  
for the model:

class Product \< ActiveRecord::Base  
def self.stocked?  
self.in\_stock ? ‘yes’ : ‘no’  
end  
end

How would I fix this?..

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [January 13, 2009, 3:40pm UTC](https://www.ruby-forum.com/t/model-methods/157599/2 "2009-01-13T15:40:53Z")

</div>

don’t think you need the ‘self’ after def.

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [January 13, 2009, 3:42pm UTC](https://www.ruby-forum.com/t/model-methods/157599/3 "2009-01-13T15:42:04Z")

</div>

On Tue, Jan 13, 2009 at 9:37 AM, sa 125  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> …  
> For example, I have a product model, and I want a method called stocked?  
> that will return ‘yes’ or ‘no’ depending on an in\_stock boolean field  
> for the model:
> 
> class Product \< ActiveRecord::Base  
> def self.stocked?  
> self.in\_stock ? ‘yes’ : ‘no’  
> end  
> end

That method should be an instance method instead of a class method,  
since  
you want to be able to ask each product whether it’s stocked.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [January 14, 2009, 7:59am UTC](https://www.ruby-forum.com/t/model-methods/157599/4 "2009-01-14T07:59:55Z")

</div>

Yup, no “self” needed since it’s an instance method:

class Product \< ActiveRecord::Base  
def stocked?  
self.in\_stock ? ‘yes’ : ‘no’  
end  
end

works great - thanks.
