Ruby on rails 自动加载常量关注点时检测到循环依赖项:<;关注点名称>;
注意:在您考虑将此问题标记为其他类似问题的副本之前,请注意此问题是关于Rails中的关注点的,而我搜索的其他问题是关于控制器的。毫无疑问,我已经发现,这与担忧有关 我在Ruby on rails 自动加载常量关注点时检测到循环依赖项:<;关注点名称>;,ruby-on-rails,ruby-on-rails-4,models,activesupport-concern,Ruby On Rails,Ruby On Rails 4,Models,Activesupport Concern,注意:在您考虑将此问题标记为其他类似问题的副本之前,请注意此问题是关于Rails中的关注点的,而我搜索的其他问题是关于控制器的。毫无疑问,我已经发现,这与担忧有关 我在app/models/concerns中有一个名为comments\u deletation.rb的文件,其中包含以下代码: module CommentsDeletion extend ActiveSupport::Concern included do after_save :delete_comments,
app/models/concerns
中有一个名为comments\u deletation.rb的文件,其中包含以下代码:
module CommentsDeletion
extend ActiveSupport::Concern
included do
after_save :delete_comments, if: :soft_deleted?
end
def soft_deleted?
status == 'deleted'
end
def delete_comments
comments.each &:destroy
end
end
class Employee < ActiveRecord::Base
include CommentsDeletion
# all the other code
end
我试图通过编写以下代码在我的模型中混合文件:
module CommentsDeletion
extend ActiveSupport::Concern
included do
after_save :delete_comments, if: :soft_deleted?
end
def soft_deleted?
status == 'deleted'
end
def delete_comments
comments.each &:destroy
end
end
class Employee < ActiveRecord::Base
include CommentsDeletion
# all the other code
end
我使用的是Rails 4.0.2,这件事让我发疯了,我无法找出我的代码出了什么问题。非常奇怪,Rails文档中没有提到以下内容,但有了它,我的代码工作起来没有任何问题 您所要做的就是将
commentsdelection
替换为关注点::commentsdelection
。否则,您必须将关注点
放在稍后要混合到模型中的模块名称之前
现在,这就是驻留在关注点目录中的模块的外观:
module Concerns::CommentsDeletion
extend ActiveSupport::Concern
included do
after_save :delete_comments, if: :soft_deleted?
end
def soft_deleted?
status == 'deleted'
end
def delete_comments
comments.each &:destroy
end
end
在我的例子中,我的代码如下所示:
#models/user.rb
class User < ApplicationRecord
include User::AuditLog
end
它在开发环境中工作得很好,但在生产环境中却出现了标题错误。当我换成这个的时候,它对我来说很好。如果与模型同名,请重命名相关文件夹名称
#models/user.rb
class User < ApplicationRecord
include Users::AuditLog
end
非常感谢您回答自己的问题。奇怪的是,这在开发中效果很好,问题是在我推进到登台时引起的。多亏了舞台表演@阿披实啊,过去的日子。现在,在Rails 5中,即使没有在开始时添加
关注点::
,它也可以完美地工作。
#model/concern/users/audit_log.rb
module Users::AuditLog
extend ActiveSupport::Concern
end