eyp
July 18, 2006, 5:46pm
1
I get this error when I try to save an object that extends of
ActiveRecord:
NoMethodError in TeamController#add_player
You have a nil object when you didn’t expect it!
The error occured while evaluating nil.keys
…
…
#{RAILS_ROOT}/app/models/player.rb:17:in create_player' #{RAILS_ROOT}/app/controllers/team_controller.rb:53:in
add_player’
And my Player model is:
class Player < ActiveRecord::Base
belongs_to :team
Crea un jugador con los valores por defecto
def Player.create_player(team_id)
player = Player.new
player.relate_to_team(team_id)
player.save <-------------------------------- LINE 17
return player
end
Relaciona el jugador con un equipo
def relate_to_team(team_id)
@team_id = team_id
end
end
What means that error? Do Player class hasn’t a save method?
eyp
July 18, 2006, 6:12pm
2
Well, the full stacktrace is:
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1557:in
attribute_names' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:2060:in
clone_attributes’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1519:in
attributes' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1945:in
attributes_with_quotes’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1725:in
update_without_lock' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/locking.rb:33:in
update_without_callbacks’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbacks.rb:274:in
update_without_timestamps' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/timestamp.rb:39:in
update’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1718:in
create_or_update_without_callbacks' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/callbacks.rb:249:in
create_or_update’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1392:in
save_without_validation' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:724:in
save_without_transactions’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:126:in
save' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in
transaction’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:91:in
transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:118:in
transaction’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/transactions.rb:126:in
save' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/base.rb:1398:in
save_without_validation!’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/validations.rb:734:in
save!' #{RAILS_ROOT}/app/models/player.rb:15:in
create_player’
#{RAILS_ROOT}/app/controllers/team_controller.rb:53:in `add_player’
And seeing the source code for base.rb, I notice it failed at:
Returns an array of names for the attributes available on this object
sorted alphabetically.
def attribute_names
@attributes.keys.sort <---------- HERE IS THE ERROR
end
What I’m doing wrong?