开发者

Adding callbacks for model classes in separate file (RoR)

I have a Message model class (which inherits from ActiveRecord::Base). For a particular deployment, I would like to have a separate file which modifies Message by adding a callback. So, instead of doing:

# app/models/message.rb
class Message < ActiveRecord::Base
  before_save :foo
  def foo
     puts 'foo!'
  end
end

I would like to be able to do:

# app/models/message.rb
class Message < ActiveRecord::Base
end

# config/initializers/fixes.rb
Message
class Message
  before_save :foo
  def foo
     puts 'foo!'
  end
end

Problem is, it works when I start the script/console, but when I start it using script/server it usually doesn't. That is the worst part, it isn't that it never works. Sometimes I start the server and it works, sometimes it doesn't, and that is without making any changes to the source.

I am restarting the server itself as (as far as I know) the initializers are run only once and don't get reloaded if modifie开发者_如何学JAVAd.

I know the 'sometimes' works is very vague, but I have spent hours here without any luck. Perhaps someone has had a similar issue, or can come up with a different idea to add the callback.


Why not put those into a module and import it?

class Message < ActiveRecord::Base
  include Message::Callbacks
end

In another file you can define whatever you like, such as message/callbacks.rb:

module Message::Callbacks
  def self.included(base)
    base.class_eval do
      before_save :foo
    end
  end

  def foo
    # ...
  end
end

The downside to this is it's more work to make the methods protected.


Why not use observers? (http://api.rubyonrails.org/classes/ActiveRecord/Observer.html)

For example, you'd do something like this:

class MessageObserver < ActiveRecord::Observer
  def before_save(message)
    puts 'you win at ruby!'
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜