hi all. I had a problem with a methods and classes what i had for example was this: the controller: class AnimalsController < ApplicationController def index @id =Animal.item end end the model: class Animal def item return 2 end end this doesn''t work because you have to maken from the Item Method a Class Method. you do this by doing def self.item. But why ? and if you have mutiple methods in it you will have to set them all to self. ?? jeljer -- 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 -~----------~----~----~----~------~----~------~--~---
Heldop Slippers wrote:> hi all. > I had a problem with a methods and classes > what i had for example was this: > > the controller: > class AnimalsController < ApplicationController > def index > @id =Animal.item > end > end > > the model: > class Animal > def item > return 2 > end > end > > this doesn''t work because you have to maken from the Item Method a Class > Method. > you do this by doing def self.item. > > But why ? and if you have mutiple methods in it you will have to set > them all to self. ?? > > jeljerYou are trying to call that method as a ''class'' level method. Adding self at the start declares a method as a class method, which is why it then worked. You probably want to be instantiating the object, so you would say def index animal = Animal.new; @id = animal.item; end -- 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 -~----------~----~----~----~------~----~------~--~---
What is the difference between a "method" inside the class ModelName < ActiveRecord::Base and a "class model" inside the same model* beside having to use "self." to make it work? *the model is a class isn''t it? On Mar 25, 11:03 pm, Dave Fumberger <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Heldop Slippers wrote: > > hi all. > > I had a problem with a methods and classes > > what i had for example was this: > > > the controller: > > class AnimalsController < ApplicationController > > def index > > @id =Animal.item > > end > > end > > > the model: > > class Animal > > def item > > return 2 > > end > > end > > > this doesn''t work because you have to maken from the Item Method a Class > > Method. > > you do this by doing def self.item. > > > But why ? and if you have mutiple methods in it you will have to set > > them all to self. ?? > > > jeljer > > You are trying to call that method as a ''class'' level method. > > Adding self at the start declares a method as a class method, which is > why it then worked. > > You probably want to be instantiating the object, so you would say > > def index > animal = Animal.new; > @id = animal.item; > end > > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Correction: I want to know the difference between a "method" and a "class method" inside a model (which is a class, isn''t it?) On Mar 26, 3:16 am, Daniel Drehmer <danieldreh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> What is the difference between a "method" inside the class ModelName < > ActiveRecord::Base and a "class model" inside the same model* beside > having to use "self." to make it work? > > *the model is a class isn''t it? > > On Mar 25, 11:03 pm, Dave Fumberger <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Heldop Slippers wrote: > > > hi all. > > > I had a problem with a methods and classes > > > what i had for example was this: > > > > the controller: > > > class AnimalsController < ApplicationController > > > def index > > > @id =Animal.item > > > end > > > end > > > > the model: > > > class Animal > > > def item > > > return 2 > > > end > > > end > > > > this doesn''t work because you have to maken from the Item Method a Class > > > Method. > > > you do this by doing def self.item. > > > > But why ? and if you have mutiple methods in it you will have to set > > > them all to self. ?? > > > > jeljer > > > You are trying to call that method as a ''class'' level method. > > > Adding self at the start declares a method as a class method, which is > > why it then worked. > > > You probably want to be instantiating the object, so you would say > > > def index > > animal = Animal.new; > > @id = animal.item; > > end > > > -- > > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 26 Mar 2008, at 06:19, Daniel Drehmer wrote:> > Correction: I want to know the difference between a "method" and a > "class method" inside a model (which is a class, isn''t it?) >When you have an instance method, you need an instance of the object. For example if I have a Person class, and Person has a last_name attribute it makes no sense to say Person.last_name, the class itself doesn''t have a last name, only instances of Person do. If bob is an instance of Person, then you can say bob.last_name and that makes perfect sense. With a class method you don''t need an instance of the class. For example if Person is an ActiveRecord class then you have a class method called find, so you can say Person.find (and writing bob.find would be a bit weird). The self. notation is because a class method is actually a singleton method on the Class. All of your classes are instances of Class, things like find and so on are singleton methods on those instances of class. Equvalently you can write class Foo class << self def a_class_method end def another_one end end end Fred> > On Mar 26, 3:16 am, Daniel Drehmer <danieldreh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> What is the difference between a "method" inside the class >> ModelName < >> ActiveRecord::Base and a "class model" inside the same model* beside >> having to use "self." to make it work? >> >> *the model is a class isn''t it? >> >> On Mar 25, 11:03 pm, Dave Fumberger <rails-mailing-l...@andreas- >> s.net> >> wrote: >> >>> Heldop Slippers wrote: >>>> hi all. >>>> I had a problem with a methods and classes >>>> what i had for example was this: >> >>>> the controller: >>>> class AnimalsController < ApplicationController >>>> def index >>>> @id =Animal.item >>>> end >>>> end >> >>>> the model: >>>> class Animal >>>> def item >>>> return 2 >>>> end >>>> end >> >>>> this doesn''t work because you have to maken from the Item Method >>>> a Class >>>> Method. >>>> you do this by doing def self.item. >> >>>> But why ? and if you have mutiple methods in it you will have to >>>> set >>>> them all to self. ?? >> >>>> jeljer >> >>> You are trying to call that method as a ''class'' level method. >> >>> Adding self at the start declares a method as a class method, >>> which is >>> why it then worked. >> >>> You probably want to be instantiating the object, so you would say >> >>> def index >>> animal = Animal.new; >>> @id = animal.item; >>> end >> >>> -- >>> 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 -~----------~----~----~----~------~----~------~--~---