脱离rails 使用Active Record

目录结构

database.yml

1 development:
2   adapter: sqlite3
3   database: db/test.db
4   pool: 5
5   timeout: 5000

001_schema.rb

 1 require ‘active_record‘
 2 class Schema <ActiveRecord::Migration
 3   def self.up
 4     create_table :customers, force: true do |t|
 5       t.string :name
 6       t.string :address
 7
 8       t.timestamps
 9     end
10   end
11
12   def self.down
13     drop_table :customers
14   end
15 end

customer.rb

1 class Customer <ActiveRecord::Base
2
3
4 end

ar.rb

 1 require ‘rubygems‘
 2 require ‘active_record‘
 3 require ‘yaml‘
 4 require ‘logger‘
 5
 6 ActiveRecord::Base.logger = Logger.new(STDOUT)
 7 dbconfig = YAML::load(IO.read(‘config/database.yml‘))
 8 ActiveRecord::Base.establish_connection(dbconfig[‘development‘])
 9
10 load ‘models/customer.rb‘

Gemfile

1 source ‘https://gems.ruby-china.org‘
2 gem ‘activerecord‘
3 gem ‘sqlite3‘
4 gem ‘rake‘

Rakefile

1 load ‘ar.rb‘
2 require ‘active_record‘
3
4 task :default => :migrate
5
6 desc ‘Run migrations‘
7 task :migrate do
8   ActiveRecord::Migrator.migrate(‘db/migrate‘, ENV[‘VERSION‘] ? ENV[‘VERSION‘].to_i : nil)
9 end

使用说明

1  在ruby目录执行 命令:

  

 1 rudy-Pc :: ~/ruby » rake
 2 D, [2016-06-15T14:36:24.712037 #6726] DEBUG -- :    (4.4ms)  CREATE TABLE "schema_migrations" ("version" varchar NOT NULL)
 3 D, [2016-06-15T14:36:24.712258 #6726] DEBUG -- :    (0.1ms)  select sqlite_version(*)
 4 D, [2016-06-15T14:36:24.716823 #6726] DEBUG -- :    (4.2ms)  CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
 5 D, [2016-06-15T14:36:24.717531 #6726] DEBUG -- :   ActiveRecord::SchemaMigration Load (0.1ms)  SELECT "schema_migrations".* FROM "schema_migrations"
 6 I, [2016-06-15T14:36:24.720448 #6726]  INFO -- : Migrating to Schema (1)
 7 D, [2016-06-15T14:36:24.720794 #6726] DEBUG -- :    (0.0ms)  begin transaction
 8 == 1 Schema: migrating ========================================================
 9 -- create_table(:customers, {:force=>true})
10 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)
11 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)
12    -> 0.0012s
13 == 1 Schema: migrated (0.0013s) ===============================================
14
15 D, [2016-06-15T14:36:24.726539 #6726] DEBUG -- :   SQL (0.1ms)  INSERT INTO "schema_migrations" ("version") VALUES (?)  [["version", "1"]]
16 D, [2016-06-15T14:36:24.731421 #6726] DEBUG -- :    (4.7ms)  commit transaction

2 在ruby目录创建 active_record.rb

  

1 load ‘ar.rb‘
2 1.upto(10) do |x|
3   customer = Customer.new
4   customer.name ="fak#{x}"
5   customer.address = ‘beijing‘
6   customer.save
7 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
时间: 2024-10-08 22:25:11

脱离rails 使用Active Record的相关文章

YII Active Record 详细解说

Active Record 虽然 Yii DAO 可以处理几乎任何数据库相关的任务, 但很可能我们会花费 90% 的时间以编写一些执行普通 CRUD(create, read, update 和 delete)操作的 SQL 语句. 而且我们的代码中混杂了SQL语句时也会变得难以维护.要解决这些问题,我们可以使用 Active Record. Active Record (AR) 是一个流行的 对象-关系映射 (ORM) 技术. 每个 AR 类代表一个数据表(或视图),数据表(或视图)的列在 A

Active Record: 資料庫遷移(Migration) (转)

Active Record: 資料庫遷移(Migration) Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook

Yii的学习(4)--Active Record

摘自Yii官网:http://www.yiiframework.com/doc/guide/1.1/zh_cn/database.ar 在官网原文的基础上添加了CDbCriteria的详细用法. 虽然 Yii DAO 可以处理几乎任何数据库相关的任务, 但很可能我们会花费 90% 的时间以编写一些执行普通 CRUD(create, read, update 和 delete)操作的 SQL 语句. 而且我们的代码中混杂了SQL语句时也会变得难以维护.要解决这些问题,我们可以使用 Active R

DAL、DAO、ORM、Active Record辨析

转自:http://blog.csdn.net/suiye/article/details/7824943 模型 Model 模型是MVC中的概念,指的是读取数据和改变数据的操作(业务逻辑).一开始我们直接把和数据库相关的代码放在模型里(sql直接写在代码中),这样就会导致以后的维护相当麻烦.业务逻辑的修改都需要开发者重新写sql,如果项目需要分库,需要将sql语句抽出来,放到单独的一层.这一层就是DAL(数据访问层). 持久层Persistence 持久层只是一个逻辑概念而已,主要任务是负责把

根据现有表操作基于active record的model

指南上都是直接生成mode,然后db migrate来生成数据库,在现实场景中,很可能是反过来的 例如 测试表app_versions rails里面,建立model class AppVersion < ActiveRecord::Base # 这个是手工添加 self.table_name = 'app_versions' end 就好了 测试controller,具体active record的查询可以多google查查 class MyTestController < Applicat

Active Record (AR) 类及实现

Active Record (AR) 是一个流行的 对象-关系映射 (ORM) 技术. 每个 AR 类代表一个数据表(或视图),数据表(或视图)的列在 AR 类中体现为类的属性,一个 AR 实例则表示表中的一行. 常见的 CRUD 操作作为 AR 的方法实现.因此,我们可以以一种更加面向对象的方式访问数据. 例如,我们可以使用以下代码向 tbl_post 表中插入一个新行. yii 表单验证规则 <?php classContactFormextendsCFormModel { public$_

yii 数据库 Active Record

// 查找满足指定条件的结果中的第一行 $post=Post::model()->find($condition,$params); // 查找具有指定主键值的那一行 $post=Post::model()->findByPk($postID,$condition,$params); // 查找具有指定属性值的行 $post=Post::model()->findByAttributes($attributes,$condition,$params); // 通过指定的 SQL 语句查找

Yii Active Record 动态数据表

Active Record(AR)是一种流行的 对象-关系映射(ORM)技术,其映射关系为 AR class:数据表 AR class property:数据表的一列 AR 实例:数据表的一条数据 所以对于常用的数据库操作(CRUD)可以转化成一种面向对象的数据操作形式. 实现一个AR类的的最简代码如下: class Post extends CActiveRecord { public static function model($className=__CLASS__) { return p

Yii1.1 Active Record 查询结果转化成数组

使用Yii 的Active Record 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: <?php // 查找满足指定条件的结果中的第一行 $post=Post::model()->find($condition,$params); // 查找具有指定主键值的那一行 $post=Post::model()->findByPk($postID,$condition,$params); // 查找具有指定属性值的行 $po