简体   繁体   中英

Patching Module in Ruby

I'm trying to add the following code that I found online as a workaround for a problem with combining MongoDB and Backbone.js in Rails, but I actually don't know Ruby/Rails that well because I'm learning all three at once.

Currently, I've just created a file in lib/ called mongoid.rb with the following content:

module Mongoid
  module BackboneSerialization
    extend ActiveSupport::Concern
    module InstanceMethods
      def serializable_hash(options = nil)
        persisted? ? super.merge('id' => _id) : super
      end
    end
  end
end

Assuming that this code is correct, is there anything else I have to be aware of to make this work. All I'm doing now is adding this code and then starting my server but that doesn't seem to solve the problem. Is there a specific place I need to store it- like lib/mongoid/backbone_serialization/instance_methods ? Or do I need to include this in certain files? If so, do I include just Mongoid or submodules?

config/environment.rb,

after require File.expand_path('../application', __FILE__)

and before APPNAME::Application.initialize!

add require "mongoid"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM