我试图将模型的不同部分引入关注。每种状态中的两个是AASM定义的状态,以及Paperclip的附件。
因此,我将相关代码移到单独的文件中。
app / models / concerns / user_aasm.rb
class User
module UserAasm
extend ActiveSupport::Concern
included do
include AASM
aasm do
state :unverified, initial: true
state :approved
state :suspended
state :deleted
state :banned
end
end
end
end
在我的user.rb中,
include UserAasm
我收到以下错误:
Unable to autoload constant UserAasm, expected app/models/concerns/user_aasm.rb to define it
我不知道我在代码中出了什么问题。如何正确使用它?
您需要像定义它。
require 'active_support/concern'
module UserAasm
extend ActiveSupport::Concern
included do
include AASM
aasm do
state :unverified, initial: true
state :approved
state :suspended
state :deleted
state :banned
end
end
end
然后在您的User
模型中
include UserAasm
这不是使模型具有外观的正确方法,因为concerns
文件夹用于放置在more之间共享的代码models
。您应该将modules
其实现为某些行为,而不是从模型中提取代码并将其放入其中concerns
阅读CodeClimate的这篇文章
引用此链接。
像这样使用mixin类似于将杂物倾倒到六个单独的垃圾抽屉中并关上门,从而“清理”一个凌乱的房间。当然,它看上去更干净,但是垃圾抽屉实际上使识别和实施澄清域模型所必需的分解和提取变得更加困难。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句