# ActiveRecord - nil object when you didn't expect it

**URL:** <https://www.ruby-forum.com/t/activerecord-nil-object-when-you-didnt-expect-it/137673>\
**Category:** Rails\
**Created:** [May 15, 2008, 7:40am UTC](https://www.ruby-forum.com/t/activerecord-nil-object-when-you-didnt-expect-it/137673 "2008-05-15T07:40:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 15, 2008, 7:40am UTC](https://www.ruby-forum.com/t/activerecord-nil-object-when-you-didnt-expect-it/137673/1 "2008-05-15T07:40:37Z")

</div>

Hi,

I have the following code in a RoR project, models and tests are all in  
the proper directories:

require File.dirname( **FILE** ) + ‘/…/test\_helper’

class GalleryTest \< Test::Unit::TestCase  
fixtures :galleries

@gallery = nil

def test\_presence\_of  
@gallery = Gallery.new  
@gallery.title = ‘My test gallery’  
@gallery.nr = 123  
image = Image.new(1, 100, ‘jpg’)  
@gallery.images = Array.new  
@gallery.images \<\< image  
@gallery.save

```
assert @gallery.valid?

```

end  
end

class Gallery \< ActiveRecord::Base  
validates\_presence\_of :title, :nr, :images  
validates\_numericality\_of :nr  
validates\_length\_of :title, :in =\> 1…255

has\_many :images  
end

class Image \< ActiveRecord::Base  
validates\_presence\_of :nr, :sort\_nr, :ext  
validates\_presence\_of :sort\_nr  
validates\_presence\_of :ext

belongs\_to :gallery

def initialize(nr, sort\_nr, ext)  
@nr = nr  
@sort\_nr = sort\_nr  
@ext = ext  
end  
end

When I run the gallery test file, ruby shows me the following error:

test\_presence\_of(GalleryTest):  
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.[]  
c:/program  
files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active\_  
record/base.rb:1871:in `read_attribute' ... record/transactions.rb:121:in`transaction’  
c:/program  
files/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.6/lib/active\_  
record/transactions.rb:129:in `save' test/unit/gallery_test.rb:23:in`test\_presence\_of’

It has something to do with gallery.images, but I can’t figure out how  
to fix this.

Another question (offtopic): Why does it take 10 seconds for the test to  
start running? This gets annoying when you want to “code a little, test  
a little”.

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 15, 2008, 11:21am UTC](https://www.ruby-forum.com/t/activerecord-nil-object-when-you-didnt-expect-it/137673/2 "2008-05-15T11:21:05Z")

</div>

On 15 May 2008, at 06:40, Aa Aa wrote:

> @nr = nr  
> @sort\_nr = sort\_nr  
> @ext = ext  
> end  
> end

You’re overriding activerecord’s initialize function, but without  
calling super, so stuff is not initialized properly (and thus crazy  
stuff happens. In addition activerecord attributes are not persisted  
as instance variables so you inititialize function doesn’t do what you  
think it does. Why do you need a custom initialize at all ?

> Another question (offtopic): Why does it take 10 seconds for the  
> test to  
> start running? This gets annoying when you want to “code a little,  
> test  
> a little”.  
> Probably because certain bits of ruby are a lot slower on windows  
> (there was another thread about this a day or 2 ago)

Fred

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 15, 2008, 12:07pm UTC](https://www.ruby-forum.com/t/activerecord-nil-object-when-you-didnt-expect-it/137673/3 "2008-05-15T12:07:23Z")

</div>

On 15 May 2008, at 11:01, Aa Aa wrote:

> my\_image.sort\_nr = 1  
> my\_image.ext = ‘jpg’
> 
> Is there another way to send arguments to initialize without doing an

Image.new :nr =\> 123, :sort\_nr =\> 1, :ext =\> ‘jpg’

Fred

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 15, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/activerecord-nil-object-when-you-didnt-expect-it/137673/4 "2008-05-15T12:01:21Z")

</div>

Frederick C. wrote:

> On 15 May 2008, at 06:40, Aa Aa wrote:
> 
> > @nr = nr  
> > @sort\_nr = sort\_nr  
> > @ext = ext  
> > end  
> > end
> 
> You’re overriding activerecord’s initialize function, but without  
> calling super, so stuff is not initialized properly (and thus crazy  
> stuff happens. In addition activerecord attributes are not persisted  
> as instance variables so you inititialize function doesn’t do what you  
> think it does. Why do you need a custom initialize at all ?
> 
> > Another question (offtopic): Why does it take 10 seconds for the  
> > test to  
> > start running? This gets annoying when you want to “code a little,  
> > test  
> > a little”.  
> > Probably because certain bits of ruby are a lot slower on windows  
> > (there was another thread about this a day or 2 ago)
> 
> Fred

Can’t test it right now, but super could be the issue indeed. I did a  
initialize override so I could do this

my\_image = Image.New(123, 1, ‘jpg’)

instead of

my\_image = Image.New  
my\_image.nr = 123  
my\_image.sort\_nr = 1  
my\_image.ext = ‘jpg’

Is there another way to send arguments to initialize without doing an  
override?

---

<div class="post-metadata">

**Author:** ![deadinplastic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@deadinplastic](https://www.ruby-forum.com/u/deadinplastic)\
**Post date:** [May 15, 2008, 7:49pm UTC](https://www.ruby-forum.com/t/activerecord-nil-object-when-you-didnt-expect-it/137673/5 "2008-05-15T19:49:37Z")

</div>

Frederick C. wrote:

> On 15 May 2008, at 11:01, Aa Aa wrote:
> 
> > my\_image.sort\_nr = 1  
> > my\_image.ext = ‘jpg’
> > 
> > Is there another way to send arguments to initialize without doing an
> 
> Image.new :nr =\> 123, :sort\_nr =\> 1, :ext =\> ‘jpg’
> 
> Fred

Thanks Fred, the test is working now. I’ve removed the initialize and  
used the =\> thing instead.
