Rails引擎 - NameError:未初始化的常量

时间:2017-04-17 15:00:35

标签: ruby-on-rails ruby ruby-on-rails-4 activesupport-concern

我正在创建一个Rails引擎分类,其中我有Class Passion和Concern Passionable。

我还有另一个引擎帖子,其中包含了Passionable Concern。此Passionable Concern属于/ lib文件夹中的引擎分类。

事实证明,每当我尝试传递一个新帖子传递passion_ids时,我都会收到以下错误消息:

  

NameError(未初始化的常量Feed :: Post :: Categorization):

     

"分类"是我在Engine中创建的连接表   分类

这段代码贴在下面。

引擎分类     #passion.rb

module Classification
  class Passion < ApplicationRecord
    ...
    has_many :categorizations
    has_many :posts, as: :categorizable, through: :categorizations
    ...
  end
end

#==========================================//
# Classification.rb (JoinTable)
module Classification
  class Categorization < ApplicationRecord
    belongs_to :passion
    belongs_to :categorizable, polymorphic: true
  end
end


#===========================================//
# classification/lib/passionable.rb

require 'active_support/concern'

module Classification
  module Passionable extend ActiveSupport::Concern
    included do
      has_many :categorizations, as: :categorizable
      has_many :passions, through: :categorizations
    end
  end
end

引擎发布

module Feed
  class Post < ApplicationRecord
    has_many :pictures, class_name: "Feed::Picture"

    include ::Classification::Passionable
    ...
  end
end

Json创建帖子。

{
    "post": {
        "title": "My post title",
        "content": "Lorem ipsum dolor sit amet..",
        "passion_ids":["8bf8559a-857d-4ca5-82e1-f93dd3effc35","d51747b7-0960-460a-b15a-4b593155e2c5"]
    }
}

错误消息:

NameError (uninitialized constant Feed::Post::Categorization):

activerecord (5.0.2) lib/active_record/inheritance.rb:152:in `compute_type'
activerecord (5.0.2) lib/active_record/reflection.rb:354:in `compute_class'
activerecord (5.0.2) lib/active_record/reflection.rb:350:in `klass'
activerecord (5.0.2) lib/active_record/reflection.rb:745:in `source_reflection'
activerecord (5.0.2) lib/active_record/reflection.rb:925:in `check_validity!'
activerecord (5.0.2) lib/active_record/associations/association.rb:25:in `initialize'
activerecord (5.0.2) lib/active_record/associations/has_many_through_association.rb:8:in `initialize'
activerecord (5.0.2) lib/active_record/associations.rb:240:in `new'
activerecord (5.0.2) lib/active_record/associations.rb:240:in `association'
activerecord (5.0.2) lib/active_record/associations/builder/collection_association.rb:66:in `passion_ids='
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:46:in `public_send'
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:46:in `_assign_attribute'
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:40:in `block in _assign_attributes'
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:39:in `each'
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:39:in `_assign_attributes'
activerecord (5.0.2) lib/active_record/attribute_assignment.rb:26:in `_assign_attributes'
activemodel (5.0.2) lib/active_model/attribute_assignment.rb:33:in `assign_attributes'
activerecord (5.0.2) lib/active_record/core.rb:319:in `initialize'
activerecord (5.0.2) lib/active_record/inheritance.rb:65:in `new'
activerecord (5.0.2) lib/active_record/inheritance.rb:65:in `new'
engines/feed/app/controllers/feed/posts_controller.rb:21:in `create'
actionpack (5.0.2) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.2) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.2) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.2) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.2) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.2) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.2) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.2) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.2) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.2) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.2) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.2) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.2) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.2) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.2) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.2) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.2) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.2) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.2) lib/abstract_controller/base.rb:126:in `process'
actionpack (5.0.2) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.2) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.2) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.2) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.2) lib/action_dispatch/routing/route_set.rb:725:in `call'
railties (5.0.2) lib/rails/engine.rb:522:in `call'
railties (5.0.2) lib/rails/railtie.rb:193:in `public_send'
railties (5.0.2) lib/rails/railtie.rb:193:in `method_missing'
actionpack (5.0.2) lib/action_dispatch/routing/mapper.rb:17:in `block in <class:Constraints>'
actionpack (5.0.2) lib/action_dispatch/routing/mapper.rb:46:in `serve'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.2) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.2) lib/action_dispatch/routing/route_set.rb:725:in `call'
rack (2.0.1) lib/rack/etag.rb:25:in `call'
rack (2.0.1) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.1) lib/rack/head.rb:12:in `call'
activerecord (5.0.2) lib/active_record/migration.rb:553:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.2) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.2) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.2) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.2) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.2) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.2) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.0.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.2) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.0.2) lib/rails/rack/logger.rb:24:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.2) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
railties (5.0.2) lib/rails/engine.rb:522:in `call'
puma (3.8.2) lib/puma/configuration.rb:224:in `call'
puma (3.8.2) lib/puma/server.rb:600:in `handle_request'
puma (3.8.2) lib/puma/server.rb:435:in `process_client'
puma (3.8.2) lib/puma/server.rb:299:in `block in run'
puma (3.8.2) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

1 个答案:

答案 0 :(得分:0)

您的关注点需要为联接表模型提供class_name选项:

# classification/lib/passionable.rb

require 'active_support/concern'

module Classification
  module Passionable extend ActiveSupport::Concern
    included do
      has_many :categorizations, as: :categorizable, class_name: 'Classification::Categorization'
      has_many :passions, through: :categorizations
    end
  end
end

因为您的模型和关注点位于不同的名称空间下。