【发布时间】:2014-04-02 21:50:45
【问题描述】:
在 app/models/abc/xyz.rb 内
module Abc::Xyz
extend ActiveSupport::Concern
end
在 app/models/abc.rb 中
class Abc < ActiveRecord::Base
include Abc::Xyz
end
当我尝试从 Abc.where(id: id) 获取数据时,有时它会起作用,有时它会返回此错误(TypeError: wrong argument type Class (expected Module))。
TypeError: wrong argument type Class (expected Module)
app/models/abc.rb:2:in `include'
app/models/abc.rb:2:in `<class:Abc>'
app/models/abc.rb:1:in `<top (required)>'
activesupport (3.2.17) lib/active_support/dependencies.rb:469:in `load'
activesupport (3.2.17) lib/active_support/dependencies.rb:469:in `block in load_file'
activesupport (3.2.17) lib/active_support/dependencies.rb:639:in `new_constants_in'
activesupport (3.2.17) lib/active_support/dependencies.rb:468:in `load_file'
activesupport (3.2.17) lib/active_support/dependencies.rb:353:in `require_or_load'
activesupport (3.2.17) lib/active_support/dependencies.rb:502:in `load_missing_constant'
activesupport (3.2.17) lib/active_support/dependencies.rb:192:in `block in const_missing'
activesupport (3.2.17) lib/active_support/dependencies.rb:190:in `each'
activesupport (3.2.17) lib/active_support/dependencies.rb:190:in `const_missing'
activesupport (3.2.17) lib/active_support/inflector/methods.rb:230:in `block in constantize'
activesupport (3.2.17) lib/active_support/inflector/methods.rb:229:in `each'
activesupport (3.2.17) lib/active_support/inflector/methods.rb:229:in `constantize'
activesupport (3.2.17) lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
【问题讨论】:
-
你能显示完整的堆栈跟踪吗?
-
我想在错误堆栈中看到(TypeError: wrong argument type Class (expected Module)),这很重要
-
我会尝试
module Xyz。关注点应该在您的模型之间共享,将它们命名为现有类是没有意义的。 -
@depa 是的,
Abc作为module和class令人困惑.. 对我和 Rails 来说也是如此...... -
如果你尝试做
class X ; end class Y include X end----你会得到同样的错误..
标签: ruby-on-rails activesupport-concern