ActiveRecord 迁移和Rake 任务未加载模型?

发布于 2024-09-11 17:05:25 字数 781 浏览 4 评论 0原文

我知道你可以这样做来加载 Rails 环境:

  task :my_task => :environment do 
    MyModel.find(1)
  end

但模型中的代码似乎没有执行。我正在使用acts_as_audited,并且有一个很好的类函数可以检索所有正在审核的模型。该调用看起来像这样:

Audit.audited_classes

要指定一个模型为可审计的,您只需将此行添加到您的模型中:

acts_as_audited

当在控制台中执行 Audited_classes 时,我会得到所有已审计类的数组;但是,当我从 rake 任务(或迁移)中执行它时,我得到一个空数组。

[编辑]

经过多一番尝试后,我注意到模型在被引用之前并未实际加载(即延迟加载)。我认为在配置中将 cache_classes 设置为 true 可以解决此问题,但它们似乎仍然是延迟加载的。

一种可能的解决方案是循环遍历所有模型(如本文中所述:有没有办法获取 Rails 应用程序中所有模型的集合?)但这似乎有点老套,我是希望有一个更清洁的方法。

有什么想法吗?

谢谢

I know that you can do something like this to load the rails environment:

  task :my_task => :environment do 
    MyModel.find(1)
  end

But it seems the code in the models are not executed. I am using acts_as_audited, and there is a nice class function which retrieves all models which are being audited. The call looks something like:

Audit.audited_classes

And to specify a model as being auditable, you simply add this line to your models:

acts_as_audited

When audited_classes is executed in console, I get an array of all my audited classes; however, when I execute it from within a rake task (or migration), I get an empty array.

[EDIT]

After playing around a bit more, I noticed that if the models are not actually loaded until they are referenced (i.e. lazy loading). I thought that setting cache_classes to true in the config would fix this, but they still seem to be lazy loaded.

One possible solution would be to loop through all the models (as explained in this post: Is there a way to get a collection of all the Models in your Rails app?) but that seems a bit hacky, and I was hoping there is a cleaner way.

Any ideas?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲世九天 2024-09-18 17:05:25

当你有 config.threadsafe 时就会发生这种情况!在生产环境中,它会自动设置 config.dependency_loading = false。这可以防止 Rails 在 rake 任务期间加载模型类。

解决这个问题的方法是在环境文件中设置“config.dependency_loading = true if $rails_rake_task”。例如,在我的 Production.rb 中,我有:

config.threadsafe!
config.dependency_loading = true if $rails_rake_task

或者你也可以这样做

config.threadsafe! unless $rails_rake_task

This happens when you have config.threadsafe! in production environments, which automatically sets config.dependency_loading = false. This prevents rails from loading your model classes during rake tasks.

The way to get around this is to set "config.dependency_loading = true if $rails_rake_task" in your environment file. For example, in my production.rb I have:

config.threadsafe!
config.dependency_loading = true if $rails_rake_task

or you can also do

config.threadsafe! unless $rails_rake_task
无可置疑 2024-09-18 17:05:25

您可以添加 config/environments/development.rb:

Dir[Rails.root.join('app', 'models', '**/*')].each { |file| File.basename(file, '.rb').camelize.constantize }

You can add config/environments/development.rb:

Dir[Rails.root.join('app', 'models', '**/*')].each { |file| File.basename(file, '.rb').camelize.constantize }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文