Reputation: 3692
We have a Rails application in which an after_create is dynamically added to an ActiveRecord model from an initializer.
We've tried instance_eval
Foo.instance_eval do
send "after_create", lambda{|r| r.some_method}
end
and class_eval
Foo.class_eval do
after_create lambda{|r| r.some_method}
end
When running Foo.new._create_callbacks
just after creating them it returns both of the callbacks.
After doing a request to the Rails server the two callbacks disappear.
This problem only happens in development. Staging and Production are working fine.
When setting the config.cache_classes = true
in the development.rb
the callbacks will persist and not disappear.
We have replicated this code in other applications and it works fine, so maybe it is a configuration issue.
NOTE: We are using subdomains with lvh.me
Thanks :)
Upvotes: 1
Views: 214
Reputation: 1317
This question seems to be a similar problem, about monkey patching in development mode, and it has a potential solution:
How to monkey-patch code that gets auto-loaded in Rails?
Upvotes: 2
Reputation: 1326
In rails during development mode, all models are reloaded when a new request comes in. This is useful because you don't have to restar the server each time you change something. However, in prod this does not happen since there is a performance penalty. The issue here is initializers only get run during initialization of the rails app. Might I ask why you are trying to set after_create in the initializer and not the model itself?
Upvotes: 0