使用多态关联时未初始化的常量

时间:2017-08-30 15:01:55

标签: ruby-on-rails ruby

我在执行时得到NameError:uninitialized constant Category :: Translation:

Category.first.translations

Rails版本: 5.1.3

Ruby版本: 2.3.3

型号:

# app/models/translation.rb
class Translation < ApplicationRecord
  belongs_to :translatable, polymorphic: true
end

# app/models/category.rb
class Category < ApplicationRecord
  has_many :translations, :as => :translatable
end

迁移:

class CreateTranslations < ActiveRecord::Migration[5.1]
  def change
    create_table :translations do |t|
      t.string :locale
      t.string :key
      t.text :value
      t.text :interpolations
      t.boolean :is_proc, default: false

      t.references :translatable, polymorphic: true, index: true

      t.timestamps
    end
  end
end

schema.rb:

create_table "translations", force: :cascade do |t|
  t.string "locale"
  t.string "key"
  t.text "value"
  t.text "interpolations"
  t.boolean "is_proc", default: false
  t.string "translatable_type"
  t.integer "translatable_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["translatable_type", "translatable_id"], name: "index_translations_on_translatable_type_and_translatable_id"
end

完整追踪:

activerecord (5.1.3) lib/active_record/inheritance.rb:166:in `compute_type'
activerecord (5.1.3) lib/active_record/reflection.rb:401:in `compute_class'
activerecord (5.1.3) lib/active_record/reflection.rb:397:in `klass'
activerecord (5.1.3) lib/active_record/associations/association.rb:127:in `klass'
activerecord (5.1.3) lib/active_record/associations/collection_association.rb:33:in `reader'
activerecord (5.1.3) lib/active_record/associations/builder/association.rb:111:in `translations'
app/controllers/categories_controller.rb:27:in `new'
actionpack (5.1.3) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.3) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.3) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.3) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.3) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.3) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.3) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.3) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.3) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.3) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.3) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.3) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:46:in `block in serve'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:834:in `call'
omniauth (1.6.1) lib/omniauth/strategy.rb:189:in `call!'
omniauth (1.6.1) lib/omniauth/strategy.rb:167:in `call'
omniauth (1.6.1) lib/omniauth/strategy.rb:189:in `call!'
omniauth (1.6.1) lib/omniauth/strategy.rb:167:in `call'
omniauth (1.6.1) lib/omniauth/builder.rb:63:in `call'
rack (2.0.3) lib/rack/etag.rb:25:in `call'
rack (2.0.3) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.3) lib/rack/head.rb:12:in `call'
rack (2.0.3) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.3) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.3) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.3) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.3) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.3) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.3) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.3) lib/rack/method_override.rb:22:in `call'
rack (2.0.3) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.3) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
railties (5.1.3) lib/rails/engine.rb:522:in `call'
puma (3.9.1) lib/puma/configuration.rb:224:in `call'
puma (3.9.1) lib/puma/server.rb:602:in `handle_request'
puma (3.9.1) lib/puma/server.rb:435:in `process_client'
puma (3.9.1) lib/puma/server.rb:299:in `block in run'
puma (3.9.1) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

我在许多网站上看到这个错误是由关联中的配置错误引起的(主要是错别字)。我还尝试设置:classname =&gt;在类别课程中的“翻译”,但这也没有帮助。我无法确定整天的问题所以我来这里寻求帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

我解决了!这与模型没有直接关系。 @MrYoshiji向我指出了正确的方向。

我使用I18n gem和i18n-active_record,所以我可以将翻译存储在数据库中。

在初始化Rails中加载了这段代码:

Translation = I18n::Backend::ActiveRecord::Translation

翻译模型是真实的I18n :: Backend :: ActiveRecord :: Translation模型。因此,我的翻译模型中的更改没有任何效果,因为该名称已链接到另一个模型..

我将app / models / translation.rb更改为:

class Translation < I18n::Backend::ActiveRecord::Translation
  belongs_to :translatable, polymorphic: true
end

并从配置中删除了上面提到的行。

无论如何,非常感谢你的评论!