# Dynamically adding class methods to Model

**URL:** <https://www.ruby-forum.com/t/dynamically-adding-class-methods-to-model/106159>\
**Category:** Rails\
**Created:** [June 6, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/dynamically-adding-class-methods-to-model/106159 "2007-06-06T23:00:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![faux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/34f0e0/32.png) [@faux](https://www.ruby-forum.com/u/faux)\
**Post date:** [June 6, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/dynamically-adding-class-methods-to-model/106159/1 "2007-06-06T23:00:59Z")

</div>

I have a Model that includes a bunch of class methods. I want to be  
able to make calls like:

calories = Nutrient.calories  
fat = Nutrient.fat

Right now, my Model looks like something I’d write in Java:

class Nutrient \< ActiveRecord::Base

def self.calories  
Nutrient.find(208)  
end

def self.fat  
Nutrient.find(204)  
end

…This goes on for awhile…  
end

I need help taking advantage of Ruby. I’d like something like:

class Nutrient \< ActiveRecord::Base  
NUTRIENTS = {“calories” =\> 208, “fat” =\> 204, …

def self.method\_missing(methodname, \*args)  
value = NUTRIENTS[methodname)  
Nutrient.find(value) unless value.nil?  
end

end

I’ve tried many permutations of dynamically adding these methods, but I  
hit a brick wall every time. Can someone give me some direction?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![faux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/34f0e0/32.png) [@faux](https://www.ruby-forum.com/u/faux)\
**Post date:** [June 6, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/dynamically-adding-class-methods-to-model/106159/2 "2007-06-06T23:48:57Z")

</div>

Look at class\_eval for creating class methods on the fly. Or extend  
the class with a module that defines method\_missing in the module.

Michael

On Jun 6, 2:00 pm, Todd R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![faux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/34f0e0/32.png) [@faux](https://www.ruby-forum.com/u/faux)\
**Post date:** [June 7, 2007, 12:36am UTC](https://www.ruby-forum.com/t/dynamically-adding-class-methods-to-model/106159/3 "2007-06-07T00:36:47Z")

</div>

MichaelLatta wrote:

> Look at class\_eval for creating class methods on the fly. Or extend  
> the class with a module that defines method\_missing in the module.
> 
> Michael
> 
> On Jun 6, 2:00 pm, Todd R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Hell yeah!

I tried class\_eval earlier but kept getting “stack too deep” errors.  
I’m not sure what I was doing wrong, but I remember it was frustrating,  
so I abandoned that approach. Your suggestion prompted me to try again

- Thank you!

Here’s what I’ve got now:

class Nutrient \< ActiveRecord::Base  
NUTRIENTS = {“calories” =\> “ENERC\_KAL”, “fat” =\> “FAT”, …

NUTRIENTS.each do | key, value |  
class\_eval “def self.#{key} ; Nutrient.find\_by\_tagname(‘#{value}’) ;  
end ;”  
end

… no more messy methods! …  
end

It’s so beautiful it makes me want to cry. Thanks again Michael!
