1
votes

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.

1
Have you checked your model's validations?jpemberthy
There are no validations the model is just class SpacialBody < ActiveRecord::Base end first thing i did was remove things like the attraccesor lineArcath

1 Answers

2
votes

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.