config.cache_classes = true 时缺少方法 [英] Method missing when config.cache_classes = true

查看:23
本文介绍了config.cache_classes = true 时缺少方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个名为 Scope 和 ScopeItem 的 ActiveRecord 模型,它们具有 has_many 关系:

I have two ActiveRecord model called Scope and ScopeItem, with a has_many relationship:

class Scope < ActiveRecord::Base
  has_many :scope_items
end

class ScopeItem < ActiveRecord::Base
  belongs_to :scope
end

我还有一个后台作业的 rake 任务来加载一堆范围(参考)数据:

I also have a rake task for a background job to load a bunch of scope (reference) data:

  task :scopes => :environment do  
      scope = Scope.create!(
          # params
      )
      5.times do |n|
        scope.scope_items.create!({
          # more params
        })
      end
    end
  end

rake 任务在开发中运行良好,但是当我转到暂存/生产设置时,任务失败,说 Scope 没有名为 scope_items 的方法.

The rake task runs fine in development, but when I move to a staging/production setup, the task fails, saying Scope does not have a method called scope_items.

我比较了两种环境中的设置,并通过实验确定如果更改以下设置,我可以在开发中重现错误:

I compared the settings in the two environments, and determined experimentally that I can reproduce the error in development if I change the following setting:

# development.rb
config.cache_classes = false 

# !development.rb
config.cache_classes = true # all others use this

在我看来,启用此设置后加载路径会发生变化.这是真的吗?如果是,是否有解决方法?

It seems to me the load path changes when this setting is turned on. Is this true, and if so, is there a workaround?

推荐答案

我们通过添加以下内容找到了 rake 任务的解决方法:

We found a workaround for the rake task by adding the following:

My::Application.configure do
  config.cache_classes = false
end
load "#{Rails.root}/app/models/scope.rb"

这会刺激环境重新加载作用域类,并且任务运行良好.

This tickles the environment to reload the scope class, and the task runs fine.

pbobb 可能已经使用了保留字 thing,尽管奇怪的是该应用程序仍然可以在所有环境中运行.

pbobb may have been on to something with the reserved word thing, although it's bizarre that the app would still work in all environments otherwise.

这篇关于config.cache_classes = true 时缺少方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆