简体   繁体   English

通过ApplicationRecord Rails扩展模块5

[英]extending Module trough ApplicationRecord Rails 5

I am trying to extend a module in the application_record.rb file in Rails 5.0. 我试图在Rails 5.0中的application_record.rb文件中扩展一个模块。

The module is located in the /lib folder: 该模块位于/ lib文件夹中:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
  extend UpdateLive
  extend UpdatePre
end

When trying to use a method in one of the Models I get the following error: 当尝试在其中一个模型中使用方法时,我收到以下错误:

NameError: uninitialized constant ApplicationRecord::UpdateLive

I can't seem to figure out what I am doing wrong, or if I forgot a step? 我似乎无法弄清楚我做错了什么,或者我忘记了一步?

You need to require update_live in your ApplicationRecord 您需要在ApplicationRecord中要求update_live

require 'update_live'

or put lib folder in the autoload path. 或者将lib文件夹放在自动加载路径中。 In application.rb : application.rb

config.autoload_paths << Rails.root.join('lib')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 5 ApplicationRecord继承与异常 - Rails 5 ApplicationRecord inheritance with exception 检查对象是否是Rails 5中的ApplicationRecord - Checking if an object is an ApplicationRecord in Rails 5 如何在 RAILS 上的 ApplicationMailer 中调用 ApplicationRecord? - How to call ApplicationRecord in ApplicationMailer on RAILS? Rails在保存时替换ApplicationRecord实例 - Rails replace ApplicationRecord instance on save 如何在Rails中获取ApplicationRecord模型的验证约束? - How to get validation constraints of ApplicationRecord model in Rails? 如何使用 Zeitwerk 在 Rails 6 中正确扩展 ApplicationRecord - How to properly extend ApplicationRecord in Rails 6 with Zeitwerk 为什么Rails 5使用ApplicationRecord而不是ActiveRecord :: Base? - Why Rails 5 uses ApplicationRecord instead of ActiveRecord::Base? Ruby on Rails 5未初始化的常量ApplicationRecord(NameError) - Ruby on rails 5 uninitialized constant ApplicationRecord (NameError) Ruby/Rails:在 ApplicationRecord 中包含关注点时的循环依赖 - Ruby/Rails: Circular dependency when including concern in ApplicationRecord 在Rails教程中,如何在没有&#39;require_relative“ application_record”&#39;的情况下从ApplicationRecord生成Article子类? - In Rails tutorial, how can Article subclass from ApplicationRecord without 'require_relative “application_record”'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM