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?... -- 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 -~----------~----~----~----~------~----~------~--~---
don''t think you need the ''self'' after def. On Tue, Jan 13, 2009 at 6:37 AM, sa 125 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>wrote:> > 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?... > -- > 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 -~----------~----~----~----~------~----~------~--~---
On Tue, Jan 13, 2009 at 9:37 AM, sa 125 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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 > endThat 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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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. -- 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 -~----------~----~----~----~------~----~------~--~---