Sunday, January 21, 2007

12 hours later fuming at Rails

This is for my fellow coders out there using Rails. I spent the better part of 4 hours trying to decipher what was wrong with a very,very simple model. I had tweaked the model some by adding some other methods and a few variables. Then I added a simple test method to exercise the creation/verification of models.

Model:
###########
class Artist < ActiveRecord::Base
require 'pp'
def initialize
end
def print
pp self.attributes { |col| col.value}
end
end

Test Case:
###########
require File.dirname(__FILE__) + '/../test_helper'

class ArtistTest < Test::Unit::TestCase
fixtures :artists
def test_create
new_artist = Artist.new
p "created artist"
new_artist.name = "name for artist"
p new_artist.print
end
end

Here's the output:
###########
test_create(ArtistTest):
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.include?
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1772:in `method_missing'
./artist_test.rb:16:in `test_create'

I went in circles for hours on this. Until I removed the empty initialize method. I'm a Java and C guy by trade. So it appears this overrides some sort of class method or constructor. But removing that fixed the problem.

Labels:

0 Comments:

Post a Comment

<< Home