美文网首页
rails-observers cannot be requir

rails-observers cannot be requir

作者: xstars | 来源:发表于2018-10-03 13:33 被阅读0次

    TL;DR: As it is mentioned in the topic line, install and require it in Gemfile if you use rails (of coz).

    I was supposed to use rails-observers gem to audit models' changes on particular case. So I added the gem into Gemfile but not require like this.

    # Gemfile
    gem 'rails-observers', require: false
    
    # config/initializers/audit.rb
    require 'rails-observers' if needed
    

    It gave me an undefined method error but worked fine if omit the require: false part.

    vendor/bundle/gems/activerecord-5.0.7/lib/active_record/dynamic_matchers.rb:21:in `method_missing': undefined method `instantiate_observers' for ActiveRecord::Base:Class (NoMethodError)
    

    After digging into the rails-observers's code, it turns out that a few monkey patches will be done when active_record is loaded (https://github.com/rails/rails-observers/blob/v0.1.5/lib/rails/observers/railtie.rb#L6-L12) which happened before all self-defined initializers loaded (https://guides.rubyonrails.org/configuring.html#initializers).

    So, as a result, require it at the beginning or let Gemfile do the requirement.

    Additional, I found a good discussion on issue to read, if interested: https://github.com/rubygems/rubygems/issues/1104

    相关文章

      网友评论

          本文标题:rails-observers cannot be requir

          本文链接:https://www.haomeiwen.com/subject/rbvzoftx.html