我有两个类,Number和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
我的其他具有has_many和belongs_to关系的类都能正常工作,但是当我调用以下内容时:
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>'
我在Freebie类上遇到了一些问题,因为它以ie结尾,而Rails试图将其单数形式变成"Freeby"而不是Freebie。但我仍然不确定为什么这不起作用。
ActiveSupport::Inflector.inflections do |inflect| inflect.irregular 'freebie', 'freebies' end
现在我可以正常调用n.freebies.create了。感谢你指引我到那里! - user1839823