Brian
2008-Nov-19 16:48 UTC
newb confusion: instance variable accessible from outside class, but nil from inside instance method
I''ve got a simple class, defined by the following table in a migration: create_table :buzzusers do |t| t.integer :user_id t.string :name t.integer :level, :default => 0 t.timestamps end I usually create it with the following Buzzuser.create(:name => "Bob") which works fine. It appears to save and load fine. I''ve written the following method within the Buzzuser class model: def promote @level += 1 end This blows up with a nil error when called. More precisely, I get: NoMethodError: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.+ However, if I''m outside the class, I can access the level variable just fine. If I do bu = Buzzuser.find(1) I can then do bu.level and it returns the correct value for level. What''s wrong? I''m assuming I''m missing something basic, but it''s pretty confusing. Thanks --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-19 16:55 UTC
Re: newb confusion: instance variable accessible from outside class, but nil from inside instance method
On 19 Nov 2008, at 16:48, Brian wrote:> What''s wrong? I''m assuming I''m missing something basic, but it''s > pretty confusing. >attributes aren''t instance variables Fred> > Thanks > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Brian
2008-Nov-19 18:19 UTC
Re: newb confusion: instance variable accessible from outside class, but nil from inside instance method
thanks for the response, but can anyone provide a simple example of the proper way to access and change an attribute from within an instance method? I''ve worked through AWDwR, re-read the ActiveRecord and Ruby sections, am currently going through wiki.rubyonrails.org and the Reference Docs, and still haven''t found it. On Nov 19, 11:55 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 19 Nov 2008, at 16:48, Brian wrote:> What''s wrong? I''m assuming I''m missing something basic, but it''s > > pretty confusing. > > attributes aren''t instance variables > > Fred > > > > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-19 18:56 UTC
Re: newb confusion: instance variable accessible from outside class, but nil from inside instance method
On Nov 19, 6:19 pm, Brian <brifo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> thanks for the response, but can anyone provide a simple example of > the proper way to access and change an attribute from within an > instance method? >You just need to call the accessor methods, same as for people outside the class. the only subtlety is that to set it you need to do self.attribute_name = value (as attribute_name = value would be interpreted as you trying to set a local variable) Fred> I''ve worked through AWDwR, re-read the ActiveRecord and Ruby sections, > am currently going through wiki.rubyonrails.org and the Reference > Docs, and still haven''t found it. > > On Nov 19, 11:55 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On 19 Nov 2008, at 16:48, Brian wrote:> What''s wrong? I''m assuming I''m missing something basic, but it''s > > > pretty confusing. > > > attributes aren''t instance variables > > > Fred > > > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Brian
2008-Nov-19 19:49 UTC
Re: newb confusion: instance variable accessible from outside class, but nil from inside instance method
ok thanks that got it working. I think I had this working before like that, but then changed it because of confusion over the usage of self. I thought self was used to define class-level methods. Apparently the usage is different in instance methods? It is partly addressed here http://wiki.rubyonrails.org/rails/pages/RubyHeadScratching On Nov 19, 1:56 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 19, 6:19 pm, Brian <brifo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > thanks for the response, but can anyone provide a simple example of > > the proper way to access and change an attribute from within an > > instance method? > > You just need to call the accessor methods, same as for people outside > the class. the only subtlety is that to set it you need to do > self.attribute_name = value (as attribute_name = value would be > interpreted as you trying to set a local variable) > > Fred > > > I''ve worked through AWDwR, re-read the ActiveRecord and Ruby sections, > > am currently going through wiki.rubyonrails.org and the Reference > > Docs, and still haven''t found it. > > > On Nov 19, 11:55 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > On 19 Nov 2008, at 16:48, Brian wrote:> What''s wrong? I''m assuming I''m missing something basic, but it''s > > > > pretty confusing. > > > > attributes aren''t instance variables > > > > Fred > > > > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-19 20:05 UTC
Re: newb confusion: instance variable accessible from outside class, but nil from inside instance method
On Nov 19, 7:49 pm, Brian <brifo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ok thanks that got it working. I think I had this working before like > that, but then changed it because of confusion over the usage of > self. I thought self was used to define class-level methods. > Apparently the usage is different in instance methods? It is partly > addressed hereSelf is simple when you get it right. It just means ''the current object''. The only reason it is relevant here is that when ruby sees self.foo = bar it can be damn sure you want to call the method foo= on the current object, whereas with foo= bar you might just want to create a local variable. in the same way def self.foo end creates a class method (in the context of a class) because it says "define a method on the object self" (as opposed to inside the appropriate class), given that at this point the self object is the class, this creates a method on that class object. so for example class Foo def self.bar puts "hi" end end is the same as class Foo def Foo.bar puts "hi" end end is the same (from the point of view of method definition - constant scopes and so on would be different) as class Foo end def Foo.bar puts "hi" end In the first example self is at that point Foo, which is why it works. It''s more convenient that typing Foo.bar partly because class names are usually longer, partly because it stands out more and partly because if you were to rename the Foo class you can leave all of your def self.bar alone whereas with the second example you would have to change def Foo.bar to def NewClassName.bar Fred> > http://wiki.rubyonrails.org/rails/pages/RubyHeadScratching > > On Nov 19, 1:56 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Nov 19, 6:19 pm, Brian <brifo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > thanks for the response, but can anyone provide a simple example of > > > the proper way to access and change an attribute from within an > > > instance method? > > > You just need to call the accessor methods, same as for people outside > > the class. the only subtlety is that to set it you need to do > > self.attribute_name = value (as attribute_name = value would be > > interpreted as you trying to set a local variable) > > > Fred > > > > I''ve worked through AWDwR, re-read the ActiveRecord and Ruby sections, > > > am currently going through wiki.rubyonrails.org and the Reference > > > Docs, and still haven''t found it. > > > > On Nov 19, 11:55 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > wrote: > > > > > On 19 Nov 2008, at 16:48, Brian wrote:> What''s wrong? I''m assuming I''m missing something basic, but it''s > > > > > pretty confusing. > > > > > attributes aren''t instance variables > > > > > Fred > > > > > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---