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 methodgenerated_methods' for nil:NilClassmethod_missing’
from /var/lib/gems/1.8/gems/activesupport-2.3.8/lib/active_support/whiny_nil.rb:52:in
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/attribute_methods.rb:356:inrespond_to?'assign_attributes’
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2906:in
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:ineach'assign_attributes’
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2902:in
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2775:inattributes='send’
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1965:in
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1965:infind_or_create_by_name'initialize’
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:2475:in
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1963:innew'find_or_create_by_name’
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1963:in
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1975:insend'method_missing’
from /var/lib/gems/1.8/gems/activerecord-2.3.8/lib/active_record/base.rb:1975:in
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.
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.