我的应用程序从 2017 年一直到现在(Rails 5.1 到 7.1.3.4)。
以前我可以随机更新控制台中的对象,例如:
object.update(variable: true)
但是我在尝试这一类时遇到了这个错误:
irb(main):008> asdf.update(expired:true)
(irb):8:in `<main>': Missing model class BananaShacks for the ExpirationDate#banana_shacks association. You can specify a different model class with the :class_name option. (NameError)
old_raise.call(*args)
^^^^^
/home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.1.3.4/lib/active_record/inheritance.rb:264:in `compute_type': uninitialized constant ExpirationDate::BananaShacks (NameError)
old_raise.call(*args)
^^^^^
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.1.3.4/lib/active_record/reflection.rb:468:in `compute_class'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.1.3.4/lib/active_record/reflection.rb:412:in `klass'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.1.3.4/lib/active_record/reflection.rb:571:in `check_validity!'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.1.3.4/lib/active_record/associations/association.rb:42:in `initialize'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.1.3.4/lib/active_record/associations.rb:320:in `new'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.1.3.4/lib/active_record/associations.rb:320:in `association'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activerecord-7.1.3.4/lib/active_record/associations/builder/association.rb:104:in `banana_shacks'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activemodel-7.1.3.4/lib/active_model/validator.rb:152:in `block in validate'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activemodel-7.1.3.4/lib/active_model/validator.rb:151:in `each'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activemodel-7.1.3.4/lib/active_model/validator.rb:151:in `validate'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activesupport-7.1.3.4/lib/active_support/callbacks.rb:426:in `block in make_lambda'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activesupport-7.1.3.4/lib/active_support/callbacks.rb:202:in `block (2 levels) in halting'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activesupport-7.1.3.4/lib/active_support/callbacks.rb:707:in `block (2 levels) in default_terminator'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activesupport-7.1.3.4/lib/active_support/callbacks.rb:706:in `catch'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activesupport-7.1.3.4/lib/active_support/callbacks.rb:706:in `block in default_terminator'
from /home/dave/.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/activesupport-7.1.3.4/lib/active_support/callbacks.rb:203:in `block in halting'
... 62 levels...
香蕉小屋:
has_many :expiration_dates
有效期
belongs_to :banana_shacks
我唯一注意到的是,错误中显示了 BananaShacks,而它应该是 BananaShack,或者至少是类名(banana_shacks / BananaShack)。
在谷歌上没有看到任何答案,除非重做数据库
belongs_to
关联必须使用单数术语。如果您在上例中对 banana_shack
模型中的 ExpirationDate
关联使用复数形式,并尝试通过 ExpirationDate.create(banana_shacks: @banana_shack)
创建实例,则会被告知有一个 "uninitialized constant ExpirationDate::BananaShacks"
。这是因为 Rails 会自动从关联名称中推断出类名称。如果关联名称的复数形式错误,那么推断出的类也将被错误地复数形式。
经过一些研究,似乎这种多元化在v6.1.0之后变得非常严格,这可能是它在升级之前工作的原因。