Hi! If i have Image class and i''d like to add ''create_thumbnail'' method to it, which will later be called from product controller, where to put this method and how to call it? I''ve tried putting it inside image controller and image model, then call it like Image.create_thumbnail, but it didn''t work - undefined method `create_thumbnail'' for Image:Class. -- Posted via http://www.ruby-forum.com/.
Jules Jacobs
2006-Mar-06 20:46 UTC
[Rails] Newbie problem with adding new method to a class
If this is an activerecord model, the create method is probably there: Image.create(:someattribute => ''value'') But if you want to define your own: class Image def self.create ''hi'' end end>> Image.create=> ''hi'' On Monday, March 06, 2006, at 9:32 PM, szymek wrote:>Hi! > >If i have Image class and i''d like to add ''create_thumbnail'' method to >it, which will later be called from product controller, where to put >this method and how to call it? > >I''ve tried putting it inside image controller and image model, then call >it like Image.create_thumbnail, but it didn''t work - undefined method >`create_thumbnail'' for Image:Class. > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsJules -- Posted with http://DevLists.com. Sign up and save your time!
Jeff Cohen
2006-Mar-06 21:02 UTC
[Rails] Re: Newbie problem with adding new method to a class
szymek wrote:> Hi! > > If i have Image class and i''d like to add ''create_thumbnail'' method to > it, which will later be called from product controller, where to put > this method and how to call it? > > I''ve tried putting it inside image controller and image model, then call > it like Image.create_thumbnail, but it didn''t work - undefined method > `create_thumbnail'' for Image:Class.Do you want a class method or an instance method? Class methods look like this: class Image def self.create_thumbnail # return a thumbnail image here # You probably need to pass some arguments to this method # so it knows what to do end end but instance methods look like this: class Image def create_thumbnail # return a thumbnail image of this object end end Notice there''s no "self." prefixed to the method name here. Jeff www.softiesonrails.com -- Posted via http://www.ruby-forum.com/.
Thanks a lot for clearing things up! -- Posted via http://www.ruby-forum.com/.
Anthony Green
2006-Mar-07 07:48 UTC
[Rails] Re: Newbie problem with adding new method to a class
> Do you want a class method or an instance method?Which is most appropriate ? Class methods can be used without being tied to any particular object. Instance methods belong to a individual object. _tony -- Posted via http://www.ruby-forum.com/.