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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Runstein <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Runstein <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---