脱离rails 使用Active Record
2016-06-15 14:43
555 查看
目录结构
database.yml
001_schema.rb
customer.rb
ar.rb
Gemfile
Rakefile
使用说明
在程序目录先执行 bundle install
1 在ruby目录执行 命令:
2 在ruby目录创建 active_record.rb
我在rubymine中直接右键单击文件,选择 run active_record
database.yml
development: adapter: sqlite3 database: db/test.db pool: 5 timeout: 5000
001_schema.rb
require 'active_record' class Schema <ActiveRecord::Migration def self.up create_table :customers, force: true do |t| t.string :name t.string :address t.timestamps end end def self.down drop_table :customers end end
customer.rb
class Customer <ActiveRecord::Base end
ar.rb
require 'rubygems' require 'active_record' require 'yaml' require 'logger' ActiveRecord::Base.logger = Logger.new(STDOUT) dbconfig = YAML::load(IO.read('config/database.yml')) ActiveRecord::Base.establish_connection(dbconfig['development']) load 'models/customer.rb'
Gemfile
source 'https://gems.ruby-china.org' gem 'activerecord' gem 'sqlite3' gem 'rake'
Rakefile
load 'ar.rb' require 'active_record' task :default => :migrate desc 'Run migrations' task :migrate do ActiveRecord::Migrator.migrate('db/migrate', ENV['VERSION'] ? ENV['VERSION'].to_i : nil) end
使用说明
在程序目录先执行 bundle install
1 在ruby目录执行 命令:
rudy-Pc :: ~/ruby » rake D, [2016-06-15T14:36:24.712037 #6726] DEBUG -- : (4.4ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL) D, [2016-06-15T14:36:24.712258 #6726] DEBUG -- : (0.1ms) select sqlite_version(*) D, [2016-06-15T14:36:24.716823 #6726] DEBUG -- : (4.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version") D, [2016-06-15T14:36:24.717531 #6726] DEBUG -- : ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations" I, [2016-06-15T14:36:24.720448 #6726] INFO -- : Migrating to Schema (1) D, [2016-06-15T14:36:24.720794 #6726] DEBUG -- : (0.0ms) begin transaction == 1 Schema: migrating ======================================================== -- create_table(:customers, {:force=>true}) DEPRECATION WARNING: `#timestamps` was called without specifying an option for `null`. In Rails 5, this behavior will change to `null: false`. You should manually specify `null: true` to prevent the behavior of your existing migrations from changing. (called from block in up at /home/rudy/ruby/db/migrate/001_schema.rb:8) D, [2016-06-15T14:36:24.722126 #6726] DEBUG -- : (0.2ms) CREATE TABLE "customers" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "address" varchar, "created_at" datetime, "updated_at" datetime) -> 0.0012s == 1 Schema: migrated (0.0013s) =============================================== D, [2016-06-15T14:36:24.726539 #6726] DEBUG -- : SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "1"]] D, [2016-06-15T14:36:24.731421 #6726] DEBUG -- : (4.7ms) commit transaction
2 在ruby目录创建 active_record.rb
load 'ar.rb' 1.upto(10) do |x| customer = Customer.new customer.name ="fak#{x}" customer.address = 'beijing' customer.save end
我在rubymine中直接右键单击文件,选择 run active_record
/home/rudy/.rbenv/versions/2.2.3/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /home/rudy/ruby/active_record.rb D, [2016-06-15T15:03:22.677823 #7646] DEBUG -- : (0.1ms) begin transaction D, [2016-06-15T15:03:22.684157 #7646] DEBUG -- : SQL (0.2ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak1"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.682053"], ["updated_at", "2016-06-15 07:03:22.682053"]] D, [2016-06-15T15:03:22.691053 #7646] DEBUG -- : (6.6ms) commit transaction D, [2016-06-15T15:03:22.691285 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.691801 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak2"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.691356"], ["updated_at", "2016-06-15 07:03:22.691356"]] D, [2016-06-15T15:03:22.698569 #7646] DEBUG -- : (6.6ms) commit transaction D, [2016-06-15T15:03:22.698767 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.699331 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak3"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.698849"], ["updated_at", "2016-06-15 07:03:22.698849"]] D, [2016-06-15T15:03:22.702730 #7646] DEBUG -- : (3.2ms) commit transaction D, [2016-06-15T15:03:22.702950 #7646] DEBUG -- : (0.1ms) begin transaction D, [2016-06-15T15:03:22.703435 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak4"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.703015"], ["updated_at", "2016-06-15 07:03:22.703015"]] D, [2016-06-15T15:03:22.706785 #7646] DEBUG -- : (3.2ms) commit transaction D, [2016-06-15T15:03:22.706989 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.707486 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak5"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.707050"], ["updated_at", "2016-06-15 07:03:22.707050"]] D, [2016-06-15T15:03:22.710844 #7646] DEBUG -- : (3.2ms) commit transaction D, [2016-06-15T15:03:22.711062 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.711585 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak6"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.711146"], ["updated_at", "2016-06-15 07:03:22.711146"]] D, [2016-06-15T15:03:22.714980 #7646] DEBUG -- : (3.2ms) commit transaction D, [2016-06-15T15:03:22.715185 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.715699 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak7"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.715263"], ["updated_at", "2016-06-15 07:03:22.715263"]] D, [2016-06-15T15:03:22.718966 #7646] DEBUG -- : (3.1ms) commit transaction D, [2016-06-15T15:03:22.719175 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.719661 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak8"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.719240"], ["updated_at", "2016-06-15 07:03:22.719240"]] D, [2016-06-15T15:03:22.722971 #7646] DEBUG -- : (3.1ms) commit transaction D, [2016-06-15T15:03:22.723230 #7646] DEBUG -- : (0.1ms) begin transaction D, [2016-06-15T15:03:22.723924 #7646] DEBUG -- : SQL (0.2ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak9"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.723325"], ["updated_at", "2016-06-15 07:03:22.723325"]] D, [2016-06-15T15:03:22.727346 #7646] DEBUG -- : (3.2ms) commit transaction D, [2016-06-15T15:03:22.727552 #7646] DEBUG -- : (0.0ms) begin transaction D, [2016-06-15T15:03:22.728065 #7646] DEBUG -- : SQL (0.1ms) INSERT INTO "customers" ("name", "address", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "fak10"], ["address", "beijing"], ["created_at", "2016-06-15 07:03:22.727617"], ["updated_at", "2016-06-15 07:03:22.727617"]] D, [2016-06-15T15:03:22.731302 #7646] DEBUG -- : (3.0ms) commit transaction Process finished with exit code 0
相关文章推荐
- ccah-500 第40题 maintain your MRv1 TaskTracker slot capacities when you migrate. What should you do
- Jenkins进阶系列之——02email-ext邮件通知模板
- Jenkins进阶系列之——01使用email-ext替换Jenkins的默认邮件通知
- LightOJ 1403 Air Raid 最小路径覆盖
- activity_main.xml页面放置TabHost控件,用于标题的选择。
- leetcode——Container With Most Water
- LeetCode:Factorial Trailing Zeroes
- [Training Video - 1] [Selenium Basics] [Xpath Selenium]
- Cookie跨域setDomain和setPath
- Annotation之标记注解
- CDISC SDTM AE domain学习笔记 - 2
- Kodi ——7 Filling containers
- mongoDB Cannot start server. The default storage engine 'wiredTiger' is not available
- 理解 OpenStack + Ceph (9): Ceph 的size/min_size/choose/chooseleaf/scrubbing/repair 等概念
- Kodi ——6 Controls (26)6.26 Panel Container
- Kodi ——6 Controls (25)6.25 FixedList Container
- xcode换新版本,原来工程编译报错:linker command failed with exit code 1(use -v to see invocation)
- Kodi ——6 Controls (24)6.24 Wraplist Container
- Kodi ——6 Controls (23)6.23 List Container
- 在AIX8.0下实现ftp上传指定文件到ftp服务器