【发布时间】:2018-06-17 01:10:57
【问题描述】:
我希望针对特定于模型的某些功能子集分离关注点。 我引用了here 并遵循了这个模式
module ModelName::ConcernName
extend ActiveSupport::Concern
included do
# class macros
end
# instance methods
def some_instance_method
end
module ClassMethods
# class methods here, self included
end
end
但是,当我尝试启动服务器时,它会导致以下错误
自动加载常量 ModelName::ConcernName 时检测到循环依赖
我想知道对模型的某些子集函数进行关注的最佳方法是什么。
编辑
提供型号代码: 路径:app/models/rent.rb
现在我的模型中有很多检查逻辑
class Rent < ActiveRecord::Base
def pricing_ready?
# check if pricing is ready
end
def photos_ready?
# check if photo is ready
end
def availability_ready?
# check if availability setting is ready
end
def features_ready?
# check if features are set
end
end
我想把它分开
class Rent < ActiveRecord::Base
include Rent::Readiness
end
并按命名空间组织关注点 路径:app/models/concerns/rent/readiness.rb
module Rent::Readiness
extend ActiveSupport::Concern
included do
# class macros
end
# instance methods
def pricing_ready?
# check if pricing is ready
end
...
module ClassMethods
# class methods here, self included
end
end
现在,如果我只使用app/models/concerns/rent_readiness.rb 中的路径进行RentReadiness 课程,我就可以正常工作了
【问题讨论】:
标签: ruby-on-rails separation-of-concerns