Directory structure
Database.yml
1 Development: 2 adapter:sqlite33 database:db/test.db4 pool:55 timeout:5000
001_schema.rb
1Require'Active_record'2 classSchema <activerecord::migration3 defSelf.up4Create_table:customers, Force:true do |t|5 T.string:name6 t.string:address7 8 T.timestamps9 EndTen End One A defSelf.down - drop_table:customers - End theEnd
Customer.rb
1 class Customer <activerecord::base234 End
Ar.rb
1Require'RubyGems'2Require'Active_record'3Require'Yaml'4Require'Logger'5 6Activerecord::base.logger =logger.new (STDOUT)7Dbconfig = Yaml::load (Io.read ('config/database.yml'))8Activerecord::base.establish_connection (dbconfig['Development'])9 TenLoad'models/customer.rb'
Gemfile
1 ' https://gems.ruby-china.org ' 2 ' ActiveRecord ' 3 ' Sqlite3 ' 4 ' Rake '
Rakefile
1Load'ar.rb'2Require'Active_record'3 4Task:d Efault =: Migrate5 6Desc'Run Migrations'7 task:migrate Do8Activerecord::migrator.migrate ('db/migrate', env['VERSION'] ? env['VERSION'].to_i:nil)9End
Instructions for use
1 Execute the command in the Ruby directory:
1RUDY-PC:: ~/Ruby»rake2D, [2016-06-15t14:36:24.712037#6726] DEBUG--: (4.4ms) CREATE TABLE "Schema_migrations" ("version" varchar not NULL)3D, [2016-06-15t14:36:24.712258#6726] DEBUG--: (0.1ms) Select Sqlite_version (*)4D, [2016-06-15t14:36:24.716823#6726] DEBUG--: (4.2ms) CREATE UNIQUE INDEX "unique_schema_migrations" On "Schema_migrations" ("Version")5D, [2016-06-15t14:36:24.717531#6726] DEBUG--: Activerecord::schemamigration Load (0.1ms) Select "Schema_migrations". * from "schema_migrations"
6I, [2016-06-15t14:36:24.720448#6726] INFO--: Migrating to Schema (1)7D, [2016-06-15t14:36:24.720794#6726] DEBUG--: (0.0MS) BEGIN TRANSACTION8= = 1 Schema:migrating ========================================================9--create_table (: Customers, {:force=>true})TenDeprecation WARNING: '#timestamps ' was called without specifying a option for ' null '. In Rails 5, this behavior'll change to ' Null:false '. You should manually specify ' Null:true ' to prevent the behavior of your existing migrations from changing. (called from block on Up At/home/rudy/ruby/db/migrate/001_schema.rb:8) OneD, [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) A-0.0012s -= = 1 schema:migrated (0.0013s) =============================================== - theD, [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 creating ACTIVE_RECORD.RB in the Ruby directory
1 ' ar.rb ' 2 1.upto (Ten) do |x| 3 Customer = customer.new4 customer.name ="fak#{x}"5 'Beijing'6 customer.save7 End
I just right-click the file in RubyMine and select 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.RBD, [2016-06-15t15:03:22.677823#7646] DEBUG--: (0.1MS) BEGIN TRANSACTIOND, [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 TRANSACTIOND, [2016-06-15t15:03:22.691285#7646] DEBUG--: (0.0MS) BEGIN TRANSACTIOND, [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 TRANSACTIOND, [2016-06-15t15:03:22.698767#7646] DEBUG--: (0.0MS) BEGIN TRANSACTIOND, [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 TRANSACTIOND, [2016-06-15t15:03:22.702950#7646] DEBUG--: (0.1MS) BEGIN TRANSACTIOND, [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 TRANSACTIOND, [2016-06-15t15:03:22.706989#7646] DEBUG--: (0.0MS) BEGIN TRANSACTIOND, [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 TRANSACTIOND, [2016-06-15t15:03:22.711062#7646] DEBUG--: (0.0MS) BEGIN TRANSACTIOND, [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 TRANSACTIOND, [2016-06-15t15:03:22.715185#7646] DEBUG--: (0.0MS) BEGIN TRANSACTIOND, [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 TRANSACTIOND, [2016-06-15t15:03:22.719175#7646] DEBUG--: (0.0MS) BEGIN TRANSACTIOND, [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 TRANSACTIOND, [2016-06-15t15:03:22.723230#7646] DEBUG--: (0.1MS) BEGIN TRANSACTIOND, [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 TRANSACTIOND, [2016-06-15t15:03:22.727552#7646] DEBUG--: (0.0MS) BEGIN TRANSACTIOND, [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 TRANSACTIONProcess finished with exit code 0
Use active Record from rails