`config.active_record.observers` syntax broken in Rails 5?
cannikin opened this issue ยท 12 comments
Trying to add an observer with the standard config.active_record.observers = [:user_observer] syntax in application.rb. But nothing ever triggers.
I was able to get them to trigger by adding an initializer with this syntax instead:
ActiveRecord::Base.add_observer UserObserver.instance
Any Rails members know of changes in Rails 5 that would have broken that config syntax?
Same here, the suggested fix works great.
I'm getting this same not-triggering behavior with a twist: it triggers in development just fine, but not in production (default Heroku setup).
I gotta go out of town for the weekend, but when I get back I'll try to replicate it
Maybe calling instance is already enough in general. In my case it fixed the failing specs. So in the example above we would add an initializer with just
UserObserver.instance
I tried that because calling add_observer on ActiveRecord::Base led to observers being added to descendants of ActiveRecord::Base in some places, which is much more general than intended
It's May 15th, 2017. I just encountered this issue. I notice there are some commits, but it seems that the PR hasn't been merged?
In addition, I tried to add an initializer with:
ActiveRecord::Base.add_observer UserObserver.instanceAnd my CI failed when loading the schema, because I call some methods like exp_changed? (ActiveModel::Dirty) in my user observer.
rake aborted!
NoMethodError: undefined method `exp_changed?' for #<ActiveRecord::InternalMetadata:0x000000064a8f70>
Did you mean? key_changed?
/home/ubuntu/change-backend/vendor/bundle/ruby/2.3.0/gems/activemodel-5.0.0/lib/active_model/attribute_methods.rb:433:in `method_missing'
/home/ubuntu/change-backend/app/models/user_observer.rb:3:in `after_save'
/home/ubuntu/change-backend/vendor/bundle/ruby/2.3.0/bundler/gems/rails-observers-206cb17bc14f/lib/rails/observers/active_model/observing.rb:352:in `update'
/home/ubuntu/change-backend/vendor/bundle/ruby/2.3.0/bundler/gems/rails-observers-206cb17bc14f/lib/rails/observers/activerecord/observer.rb:118:in `block (2 levels) in define_callbacks'
My solution is to add observers directly to my model:
User.add_observer UserObserver.instanceThen CI passed. But I met some deployment issues. I guess it's caused by the loading sequence - I'm calling a model in the initializers.
Finally, I move that line to the end of my User model file. Then it seems everything goes well. Well I still feel so wrong...shouldn't I use callbacks instead?
class User < ApplicationRecord
# snippets
end
User.add_observer UserObserver.instanceI just experienced this issue with Rails 4.2.8. The fix was to add MyObserver.instance to an initializer. With that, tests pass.
Just encountered this issue with Rails 5 upgrade and already using 0.1.5. Also fixed by calling MyObserver.instance in the initializer.
Just encountered this issue with Rails 5 upgrade and already using 0.1.5. Also fixed by calling
MyObserver.instancein the initializer.
Hi,
Sorry, I am new to rails.
Can you please explain this a bit more? Thanks.
Do I need to create new initializer under config/initializers ?
Regards,
Gaurav