【发布时间】:2018-09-15 11:51:33
【问题描述】:
在我的 rails 5 应用程序中(在开发环境中),我有一些模块要从 lib 文件夹中加载并包含在模型中。所以我在我的 application.rb
中设置config.autoload_paths += %W(#{config.root}/lib)
模块类似于
lib/surveyable/surveyable.rb
require 'active_support/concern'
module Surveyable
extend ActiveSupport::Concern
class_methods do
def sjs_elements(&block)
....
end
end
end
包含在我的用户模型类中:
app/models/user.rb
class User < ApplicationRecord
include Surveyable # <= this doesn't raise any error
sjs_elements do # <= *** NameError Exception: undefined local variable or method `sjs_elements' for User (call 'User.connection' to establish a connection):Class
....
end
....
end
我需要在 application.rb 的开头手动要求它运行,但这违反了 Rails 约定:
require_relative '../lib/surveyable/surveyable'
【问题讨论】:
标签: ruby-on-rails