how to require active record working outside of rails

前端 未结 2 716
不思量自难忘°
不思量自难忘° 2020-12-05 03:01

i need to require active record, but I am working outside of rails (here is why: Simple Ruby Input Validation Library). do I need to require the entire rails gem, or can i b

相关标签:
2条回答
  • 2020-12-05 03:13

    Here's how I'm using ActiveRecord outside of Rails:

    #!/usr/bin/ruby
    
    require 'active_record'
    require 'mysql2' # or 'pg' or 'sqlite3'
    
    ActiveRecord::Base.establish_connection(
      adapter:  'mysql2', # or 'postgresql' or 'sqlite3'
      database: 'DB_NAME',
      username: 'DB_USER',
      password: 'DB_PASS',
      host:     'localhost'
    )
    
    # Note that the corresponding table is 'orders'
    class Order < ActiveRecord::Base
    end
    
    Order.all.each do |o|
      puts "o: #{o.inspect}"
    end
    
    0 讨论(0)
  • 2020-12-05 03:13
    require 'rubygems'
    require 'active_record'
    
    0 讨论(0)
提交回复
热议问题