Re-open an ActiveRecord model that's provided by a gem

后端 未结 6 2060
旧时难觅i
旧时难觅i 2021-02-10 05:53

I\'m trying to extend an ActiveRecord model (Vote) that a gem (https://github.com/peteonrails/vote_fu) provides to my application. (I.e., there is no vote.rb<

6条回答
  •  离开以前
    2021-02-10 06:47

    [UPDATE: should be the right solution to prevent the module being include several times in the same class]

    I believe you can use ActiveSupport::Concern to prevent the module being include several times which result by callback called several time. See the example below :

    module VotePatch
      extend ActiveSupport::Concern
    
      included do
        after_create :create_activity_stream_event
        has_one :activity_stream_event
      end
    
      module InstanceMethods
        def create_activity_stream_event
          #your code here
        end  
      end
    
    end
    
    Vote.send(:include, VotePatch)
    

提交回复
热议问题