1

我正在尝试在我的 rails 应用程序中设置 Employee 模型和 NetworkDrive 模型之间的 has_and_belongs_to_many 关系。

在 employee.rb 我指定

has_and_belongs_to_many :network_drives</code>

并在network_drive.rb ...

has_and_belongs_to_many :employee</code>

但是,它似乎正在生成像“:network_drife_ids”而不是“:network_drive_ids”这样的模型属性,这给了我这样的错误

uninitialized constant Employee::NetworkDrife

这是准确的,考虑到模型称为 NetworkDrive,而不是 NetworkDrive。

对不起,如果这是一个重复的问题,但我不知道如何搜索这个问题。我在这里尝试了关于 HABTM 关系的问题提供的几种解决方案,但无济于事。

更新:这是整个错误,在尝试将其更改为 as has_many :through 关系之后。

NameError: uninitialized constant EmployeeItRequest::EmployeeItRequestDrife
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.7/lib/active_record/inheritance.rb:111:in `compute_type'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.7/lib/active_record/reflection.rb:172:in `klass'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.7/lib/active_record/reflection.rb:216:in `association_primary_key'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.7/lib/active_record/associations/has_many_association.rb:104:in `foreign_key_present?'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.7/lib/active_record/associations/association.rb:165:in `find_target?'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.7/lib/active_record/associations/collection_association.rb:332:in `load_target'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.7/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.7/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.7/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.7/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.7/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

更新:NetworkDrive.tableize 的结果

NoMethodError: undefined method `tableize' for #<Class:0x0000000229fef8>
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/activerecord-3.2.7/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
    from (irb):1
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.7/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.7/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rvm/gems/ruby-1.9.2-p320/gems/railties-3.2.7/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
4

1 回答 1

7

这不是 HABTM 的问题,而是 rails 的内置变形规则之一,它转换drives为单数drife而不是drive.

在控制台中:

"drives".singularize  # => "drife"

您可以通过添加变形规则来覆盖默认格式:

初始化器/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.clear :inflection_drives
  inflect.irregular 'network_drive', 'network_drives'
end
于 2012-08-29T20:55:28.637 回答