Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: uninitialized constant -- activerecord

I have two classes, Number and Freebie:

class Number < ActiveRecord::Base
  belongs_to :account
  has_many :freebies

end

class Freebie < ActiveRecord::Base
  belongs_to :number

  attr_accessible :name, :data

  has_attached_file :data, :path => "freebies/:id_partition/:filename"

  def to_s
    name
  end
end

All of my other classes with a has_many and belongs_to relationship work fine, but when I call something like:

n = Number.last
n.freebies.create

I get this error:

NameError: uninitialized constant Number::Freeby
    from /Users/Robby/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.8/lib/active_record/inheritance.rb:111:in `compute_type'
    from /Users/Robby/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.8/lib/active_record/reflection.rb:172:in `klass'
    from /Users/Robby/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.8/lib/active_record/associations/collection_association.rb:148:in `transaction'
    from /Users/Robby/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.8/lib/active_record/associations/collection_association.rb:431:in `create_record'
    from /Users/Robby/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.8/lib/active_record/associations/collection_association.rb:119:in `create'
    from /Users/Robby/.rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:46:in `create'
    from (irb):20
    from /Users/Robby/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
    from /Users/Robby/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
    from /Users/Robby/.rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

I've had some other problems with the Freebie class because it ends with ie and Rails tries to make the singular version "Freeby" instead of Freebie. Still, I'm not sure why this isn't working.

like image 694
user1839823 Avatar asked Aug 13 '26 02:08

user1839823


1 Answers

Rails automatically tries to infer the plural version of a word from its singular form and vice-versa. You might want to look into overriding the default inflection rules.

Try something similar to the answer in this Stackoverflow question.

like image 168
Zajn Avatar answered Aug 14 '26 21:08

Zajn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!