如何在application.rb中定义rails观察者路径?

时间:2013-07-20 05:47:45

标签: ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我只想在单行中定义我的所有观察者现在我有这样的代码config.active_record.observers = :user_observer, :studet_observer, :suggestion_observer, :progress_observer, :admin_observer, :employee_observer, :update_message_observer, :test_observer, :question_observer, :exam_observer, :coach_observer, :wordpress_post_observer, :answer_observer , :doubt_observer, :doubt_reply_observer, :image_format_observer #FIXME ,这是否可以定义为config.active_record.observers_path = /app/observers

2 个答案:

答案 0 :(得分:0)

只需在config / application.rb

中执行config.autoload_paths += [#{config.root}/app/observers]

答案 1 :(得分:0)

我使用Dir,文件功能,它的工作

来做到这一点
config.active_record.observers = Dir["#{Rails.root}/app/observers/*.rb"].map{|file| File.basename(file, '.rb').to_sym}