我正在尝试在我的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,而不是NetworkDrife。
很抱歉,如果这是一个重复的问题,但我不知道如何搜索此问题。我在这里尝试了几个关于HABTM关系问题的解决方案,但无济于事。
更新:这是完整的错误,在尝试将其更改为has_many之后:通过关系。
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>'
答案 0 :(得分: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