I''ve got a simple model class ExtraType < ActiveRecord::Base has_many :match_balls attr_accessor :extra_total def initialize self.extra_total=0 end end When I try ex=ExtraType.new, I get INTERNAL ERROR!!! You have a nil object when you didn''t expect it! The error occurred while evaluating nil.has_key? /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/ base.rb:2633:in `has_attribute?'' I don''t think I''m doing anything daft here .. or am I (It''s the end of a long week after all) Ruby 1.8.6 with Rails 2.2.2
2009/6/19 Martin Hawkins <martin.hawkins-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > I''ve got a simple model > > class ExtraType < ActiveRecord::Base > has_many :match_balls > attr_accessor :extra_total > def initialize > self.extra_total=0 > end > end > > When I try > ex=ExtraType.new, I get > INTERNAL ERROR!!! You have a nil object when you didn''t expect it! > The error occurred while evaluating nil.has_key? > /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/ > base.rb:2633:in `has_attribute?'' > > I don''t think I''m doing anything daft here .. or am I (It''s the end of > a long week after all)I think you should call super in initialize otherwise it is not doing the base class initialize. Also watch out as I think rails does not always call new to instantiate an object so your initialize may not get called. See http://blog.dalethatcher.com/2008/03/rails-dont-override-initialize-on.html for example. Colin> Ruby 1.8.6 with Rails 2.2.2 > > >
Many thanks. Calling super fixed it. Doh - should have realised that - as I said - end of a long week. On 19 June, 17:35, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/6/19 Martin Hawkins <martin.hawk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > > I''ve got a simple model > > > class ExtraType < ActiveRecord::Base > > has_many :match_balls > > attr_accessor :extra_total > > def initialize > > self.extra_total=0 > > end > > end > > > When I try > > ex=ExtraType.new, I get > > INTERNAL ERROR!!! You have a nil object when you didn''t expect it! > > The error occurred while evaluating nil.has_key? > > /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/ > > base.rb:2633:in `has_attribute?'' > > > I don''t think I''m doing anything daft here .. or am I (It''s the end of > > a long week after all) > > I think you should call super in initialize otherwise it is not doing > the base class initialize. Also watch out as I think rails does not > always call new to instantiate an object so your initialize may not > get called. Seehttp://blog.dalethatcher.com/2008/03/rails-dont-override-initialize-o... > for example. > > Colin > > > Ruby 1.8.6 with Rails 2.2.2