Rails extending ActiveRecord::Base

后端 未结 9 1650
眼角桃花
眼角桃花 2020-11-22 12:38

I\'ve done some reading about how to extend ActiveRecord:Base class so my models would have some special methods. What is the easy way to extend it (step by step tutorial)?<

9条回答
  •  萌比男神i
    2020-11-22 13:13

    Step 1

    module FooExtension
      def foo
        puts "bar :)"
      end
    end
    ActiveRecord::Base.send :include, FooExtension
    

    Step 2

    # Require the above file in an initializer (in config/initializers)
    require 'lib/foo_extension.rb'
    

    Step 3

    There is no step 3 :)
    

提交回复
热议问题