gpt4 book ai didi

mysql - Rails has_and_belongs_to_many

转载 作者:行者123 更新时间:2023-11-29 04:02:06 26 4
gpt4 key购买 nike

我正在尝试在我的 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”这样的模型属性,这给我带来了如下错误

未初始化的常量 Employee::NetworkDrife

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

如果这是一个重复的问题,我很抱歉,但我不确定如何搜索这个问题。我在这里尝试了有关 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>'

最佳答案

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

在控制台中:

"drives".singularize  # => "drife"

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

initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
inflect.clear :inflection_drives
inflect.irregular 'network_drive', 'network_drives'
end

关于mysql - Rails has_and_belongs_to_many,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12186288/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com