在environment.rb中加载观察者(在模型子目录中)的语法?

发布于 2024-09-04 09:25:06 字数 622 浏览 2 评论 0原文

我有很多模型观察者,所以我想将它们组织在模型文件夹下的子目录中。

model --> observer --> user_observer.rb, activity_observer.rb, etc.

在放入子目录之前,我像这样加载观察者:

Rails::Initializer.run do |config|
   config.active_record.observers = :user_observer, :activity_observer
end

现在,当我尝试启动服务器时,出现以下错误:

`load_missing_constant': Expected /Users/cscairns/source/myapp/app/models/observer/user_observer.rb to define UserObserver (LoadError)

现在观察者看起来像这样,我需要在environment.rb中使用的正确语法是什么?

Observer::UserObserver < ActiveRecord::Observer

I have a lot of model observers, so I want to organize them in a subdirectory under the model folder.

model --> observer --> user_observer.rb, activity_observer.rb, etc.

Prior to placing in the subdirectory, I was loading the observers like this:

Rails::Initializer.run do |config|
   config.active_record.observers = :user_observer, :activity_observer
end

Now when I try to launch the server, I get the following error:

`load_missing_constant': Expected /Users/cscairns/source/myapp/app/models/observer/user_observer.rb to define UserObserver (LoadError)

What is the proper syntax that I need to use in environment.rb now that the observers look like?

Observer::UserObserver < ActiveRecord::Observer

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

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

发布评论

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

评论(2

梦里人 2024-09-11 09:25:06

根据 keruilin 的回答,该页面不再存在,但其声明的代码已不复存在:

# config/environment.rb
Dir.chdir("#{Rails.root}/app/models") do
  config.active_record.observers = Dir["*_observer.rb"].collect {|ob_name|         ob_name.split(".").first}
end

对于那些喜欢根级“app/observers”文件夹的人:

# config/environment.rb
config.load_paths += "#{Rails.root}/app/observers"

Dir.chdir("#{Rails.root}/app/observers") do
  config.active_record.observers = Dir["*_observer.rb"].collect {|ob_name| ob_name.split(".").first}
end

Per keruilin's answer, the page no longer exists but its stated code:

# config/environment.rb
Dir.chdir("#{Rails.root}/app/models") do
  config.active_record.observers = Dir["*_observer.rb"].collect {|ob_name|         ob_name.split(".").first}
end

and for those who prefer a root-level 'app/observers' folder:

# config/environment.rb
config.load_paths += "#{Rails.root}/app/observers"

Dir.chdir("#{Rails.root}/app/observers") do
  config.active_record.observers = Dir["*_observer.rb"].collect {|ob_name| ob_name.split(".").first}
end
你如我软肋 2024-09-11 09:25:06

看起来可以在这里找到答案: http:// handyrailstips.com/tips/4-loading-observers-automatically-from-their-own-folder


内容复制自 web.archive.org,存档于 2009-05-25 19:36:17

自动从自己的文件夹加载观察者

作者:gavin

观察者是清理和干燥模型的好方法。我经常使用它们来处理发送邮件、记录消息以及其他严格来说不属于模型职责的事情。

自从我发现观察者以来,困扰我的一件事是默认情况下不会调用它们。相反,您必须明确声明应该加载哪些观察者(通常在environment.rb中)

如果您计划在应用程序启动时从环境中加载所有观察者(这很可能),那么您可以将以下内容添加到您的环境中。 rb 文件以在您启动应用程序时自动加载它们:

# config/environment.rb
Dir.chdir("#{Rails.root}/app/models") do
  config.active_record.observers = Dir["*_observer.rb"].collect {|ob_name| ob_name.split(".").first}
end

这看起来有点混乱,但您在这里所做的只是在 models 文件夹中查找所有观察者文件的名称,然后配置您的应用程序以加载其中每个

config.active_record.observers =

文件让我对观察者感到困扰的是,它们默认存储在模型文件夹中。如果您正在构建一个包含多个模型和多个观察者的大型应用程序,这可能会变得非常混乱和混乱。

为了解决这个问题,我通常在 app/ 中创建一个名为“observers”的新文件夹。然后,我将创建的所有观察者移动到此文件夹中。

为了确保在启动时加载此文件夹,我将以下内容添加到environment.rb

config.load_paths += "#{Rails.root}/app/observers"

简单!

如果您决定将其与上一个自动加载观察者的技巧一起使用,那么您必须将正在查找的目录名称从 models 更改为 observers

# config/environment.rb
Dir.chdir("#{Rails.root}/app/observers") do
  config.active_record.observers = Dir["*_observer.rb"].collect {|ob_name| ob_name.split(".").first}
end

记住在创建新观察者或对environment.rb进行任何更改后重新启动应用程序

Looks like answer can be found here: http://handyrailstips.com/tips/4-loading-observers-automatically-from-their-own-folder.


Content Copied from web.archive.org, archived on 2009-05-25 19:36:17

Loading Observers Automatically From Their Own Folder

by gavin

Observers are a great way of cleaning up and DRYing up your models. I use them quite a lot to deal with sending mails, logging messages and anything else that isn’t strictly the model’s responsibility.

One thing that’s bugged me since I discovered observers is that they are not invoked by default. Instead you have to explicitly state which observers should be loaded (usually in environment.rb)

If you plan on loading all of your observers from the environment when your app is booted (which is likely) then you can add the following to your environment.rb file to load them all automatically when you start your app:

# config/environment.rb
Dir.chdir("#{Rails.root}/app/models") do
  config.active_record.observers = Dir["*_observer.rb"].collect {|ob_name| ob_name.split(".").first}
end

This looks a little messy but all you are doing here is simply finding the names of all the observer files in the models folder and configuring your app to load each of them with

config.active_record.observers =

One other thing that bugs me about observers it that they are stored in the models folder by default. If you’re building a big app with several models and several observers this can get pretty cluttered and confusing.

To get around this I usually create a new folder in app/ called ‘observers’. I then move any observers I create into this folder.

To make sure this folder is loaded at startup I add the following to environment.rb

config.load_paths += "#{Rails.root}/app/observers"

Simple!

If you decide to use this along with the previous tip on loading the observers automatically then you’ll have to change the name of the directory you’re looking in from models to observers:

# config/environment.rb
Dir.chdir("#{Rails.root}/app/observers") do
  config.active_record.observers = Dir["*_observer.rb"].collect {|ob_name| ob_name.split(".").first}
end

Remember to reboot your app after creating a new observer or making any changes to environment.rb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文