views:

81

answers:

2

Ive got a model caleld SpacialBody, and I need to seed some records so first off I added this to my seeds.rb

[
    ["Sol",0,0,0,"standard"]
].each do |body|
    nb=SpacialBody.find_or_create_by_name(body[0])
    nb.name = body[0]
    nb.x = body[1]
    nb.y = body[2]
    nb.type = SpacialBody::Types[body[3]]
    nb.class = body[4]
    nb.save
end

and this produced an error. I then went into console to test the code and found that this happened:

SpacialBody.new => # SpacialBody.find_by_name("Sol") => nil SpacialBody.find_or_create_by_name("Sol") NoMethodError: undefined method generated_methods' for nil:NilClass from /var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/whiny_nil.rb:52:inmethod_missing' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/attribute_methods.rb:356:in respond_to?' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:inassign_attributes' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in each' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:inassign_attributes' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2775:in attributes=' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1965:insend' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1965:in find_or_create_by_name' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2475:ininitialize' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1963:in new' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1963:infind_or_create_by_name' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1975:in send' from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1975:inmethod_missing' from (irb):3

Ive used find_or_create_by_field in other projects without incedent, and i cant see anything different in my setup here.

Its only this model that has the problem, others in the same project work fine.

A: 

"name" is a reserved word in ruby.

SpacialBody.find_or_create_by_name("Sol")

This might be blowing up due to the use of "name". Try changing it.

Otherwise, the code that you posted looks fine.

nicholasklick
unfourtunatley name is the field i want to search on.... o well i will have to change the field with a migration
Arcath
Nope, still doesn't work, even changed the field to a random word
Arcath
A: 

facepalm

using class and type as fields in the model.... not a good move

both are reserved names which cause ActiveRecord to fail when building the methods.

Arcath