ActiveRecord create table
cat app.rb<br />
require 'active_record'ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'datasetter_development')
#连接本地的数据库
class User < ActiveRecord::Base<br />
end
#再次运行创建table数据的时候,必须存在该表数据的class,进行migrate的时候可以不必存在。
class Account < ActiveRecord::Base<br />
end
class CreateAccountTable < ActiveRecord::Migration[5.2]<br />
def change<br />
create_table :accounts do |table|<br />
table.string :first_name<br />
table.integer :last_name<br />
table.timestamps null: false<br />
end<br />
end<br />
end
# Create the table
#运行一次ruby app.rb 就会进行migrate,再次运行脚本的时候,就需要把创建table的语句注释掉,不然会报错,提示该表已经存在了<br />
#CreateAccountTable.migrate(:up)
# Drop the table<br />
#CreateAccountTable migrate(:down)
<p>#创建数据,十万条数据</p>
(1..10000).each do |i|<br />
Account.create first_name: "jim", last_name: "#{('A'..'Z').to_a.shuffle[0,5].join}"<br />
puts Account.all.size<br />
end
(1..10000).each do |i|<br />
Account.create last_name: "jim", first_name: "#{('A'..'Z').to_a.shuffle[0,5].join}"<br />
puts Account.all.size<br />
end
(1..80000).each do |i|<br />
Account.create first_name: "#{('A'..'Z').to_a.shuffle[0,5].join}", last_name: "#{('A'..'Z').to_a.shuffle[0,5].join}"<br />
puts Account.all.size<br />
end
</p>