1

I'm quite new to RoR and I got a question regarding initialization of libraries. how do I load up a class I extended from an existing class library in rails?

I'd like to extend the ActiveRecord::base as below based on this link

# lib/active_record/add_reset_pk_sequence_to_base.rb
module ActiveRecord
  class Base
    def self.reset_pk_sequence
      case ActiveRecord::Base.connection.adapter_name
      when 'SQLite'
        new_max = maximum(primary_key) || 0
        update_seq_sql = "update sqlite_sequence set seq = #{new_max} where name = '#{table_name}';"
        ActiveRecord::Base.connection.execute(update_seq_sql)
      when 'PostgreSQL'
        ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
      else
        raise "Task not implemented for this DB adapter"
      end
    end     
  end
end

On purpose, I would like to use the extended class in seed.rb to reset auto increment of tables. how do I load the file with the 'require' statement? I have tried several ways, unfortunately, things didn't work out fine for me?

any advice would be very much appreciated?

Community
  • 1
  • 1
Sarun Sermsuwan
  • 3,608
  • 5
  • 34
  • 46

1 Answers1

2

Put the file in config/initializers folder or load it using require from that folder..

Jasdeep Singh
  • 3,276
  • 4
  • 28
  • 47
  • That doesn't seem like the right use of an initializer since its just monkey patching active record. I thought initializers were for setting the state of things such as configurations. – Peter Brown Mar 04 '12 at 11:59