每天一剂Rails良药之Use ActiveRecord Outside of Rails

ActiveRecord属于Rails的子框架,我们可以在Rails外使用它
process_orders_nightly.rb:
require 'active_record'
ActiveRecord::Base.establish_connection(
  :adapter => "mysql",
  :host => "localhost",
  :username => "nightlybatch",
  :password => "secret",
  :database => "web_orders"
)

class Order < ActiveRecord::Base
end
ActiveRecord::Base.logger = Logger.new(STDOUT)

Order.find(:all).each do |o|
  puts "Processing order number #{o.id}"
  './sendorder -c #{o.customer_id} \
    -p #{o.product_id} \
    -q #{o.quantity}'
end

你可能感兴趣的:(mysql,框架,Web,Rails,ActiveRecord)