Topic: "NoMethodError" while running Rake

Hi All,
I am totaly new to web development and Ruby.

I am following Apress-Ruby on Rails E-Commerce book. (in Windows)
The book is based on some earlier version of Ruby.

My current versions are
------------------------
Ruby version            : 1.8.6 (i386-mswin32)
RubyGems version        : 1.0.1
Rails version           : 2.0.2
Active Record version   : 2.0.2
Action Pack version     : 2.0.2
Active Resource version : 2.0.2
Action Mailer version   : 2.0.2
Active Support version  : 2.0.2
Database adapter        : mysql
Database schema version : 1
Rake version            : 0.8.4
OS : WinXP 32bit
---------------------------

I am stuck @ page 36-37 of the book.

File : test/unit/author_test.rb

require File.dirname(__FILE__) + '/../test_helper'
class AuthorTest < Test::Unit::TestCase
  fixtures :authors
  def test_name
    author = Author.create(:first_name => 'Joel',:last_name => 'Spolsky')
    assert_equal 'Joel Spolsky', author.name
  end
end

File : app/models/author.rb
class Author < ActiveRecord::Base
   def name
    "#{first_name} #{last_name}"
  end
end

This code runs properly when I run on my laptop but not on my desktop. (I dont know what is different)

"rake db:migrate" runs fine.

Running "Rake" gives error shown below...

C:\_irails\rails_apps\emporium>rake
(in C:/_irails/rails_apps/emporium)
C:/_irails/ruby/bin/ruby -I"C:/_irails/rails_apps/emporium/lib" -I"C:/_irails/ra
ils_apps/emporium/test" "C:/_irails/ruby/lib/ruby/gems/1.8/gems/rake-0.8.4/lib/r
ake/rake_test_loader.rb" "test/unit/author_test.rb"
Loaded suite C:/_irails/ruby/lib/ruby/gems/1.8/gems/rake-0.8.4/lib/rake/rake_tes
t_loader
Started
E
Finished in 0.078 seconds.

  1) Error:
test_name(AuthorTest):
NoMethodError: undefined method `first_name=' for #<Author id: nil, created_at:
nil, updated_at: nil>
    C:/_irails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
attribute_methods.rb:200:in `method_missing'
    C:/_irails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
base.rb:2117:in `send'
    C:/_irails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
base.rb:2117:in `attributes='
    C:/_irails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
base.rb:2116:in `each'
    C:/_irails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
base.rb:2116:in `attributes='
    C:/_irails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
base.rb:1926:in `initialize'
    C:/_irails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
base.rb:571:in `new'
    C:/_irails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
base.rb:571:in `create'
    ./test/unit/author_test.rb:6:in `test_name'

1 tests, 0 assertions, 0 failures, 1 errors
C:/_irails/ruby/bin/ruby -I"C:/_irails/rails_apps/emporium/lib" -I"C:/_irails/ra
ils_apps/emporium/test" "C:/_irails/ruby/lib/ruby/gems/1.8/gems/rake-0.8.4/lib/r
ake/rake_test_loader.rb" "test/functional/about_controller_test.rb"
Loaded suite C:/_irails/ruby/lib/ruby/gems/1.8/gems/rake-0.8.4/lib/rake/rake_tes
t_loader
Started
.
Finished in 0.063 seconds.

1 tests, 1 assertions, 0 failures, 0 errors
C:/_irails/ruby/bin/ruby -I"C:/_irails/rails_apps/emporium/lib" -I"C:/_irails/ra
ils_apps/emporium/test" "C:/_irails/ruby/lib/ruby/gems/1.8/gems/rake-0.8.4/lib/r
ake/rake_test_loader.rb"
Errors running test:units!

Please guys, help me out.
I'll be thankful to you a lot.

Re: "NoMethodError" while running Rake

gohilurvish,

this line:

NoMethodError: undefined method `first_name=' for #<Author id: nil, created_at:
nil, updated_at: nil>

indicates that your Author model currently does not have a field "first_name". Please check whether the database has been setup correctly, maybe you neglected a
rake db:migrate

or a
rake db:test:clone

prior to running your tests.

Lead Developer of the WebNames Project and the wnrp:// protocol handler
http://we.bnam.es
wnrp://twitter.com

Re: "NoMethodError" while running Rake

Thanks NoJ for your instant reply. (this was the fastes reply I have seen...)

I did run "rake db:migrate" which ran fine.

Here is app/models/author.rb :

class Author < ActiveRecord::Base
    def name
        "#{first_name} #{last_name}"
    end
end

Re: "NoMethodError" while running Rake

my pleasure (:

Did you also run

rake db:test:clone

or
rake db:test:clone_structure

?
Which DB are you using? MySQL, Sqlite, Postgres? You might want to manually check whether the tables have been created correctly.

How does your migration for creating the "Author" Model look like? As the problem is not with your app/modesls/author.rb ( which is fine ), but with the underlying DB structure.
While doing this

author = Author.create(:first_name => 'Joel',:last_name => 'Spolsky')

your application complains about
NoMethodError: undefined method `first_name=' for #<Author id: nil, created_at:
nil, updated_at: nil>

which simply means that in your DB, no column called "first_name" exists.

Lead Developer of the WebNames Project and the wnrp:// protocol handler
http://we.bnam.es
wnrp://twitter.com