· 8 years ago · May 27, 2018, 01:18 AM
1require 'rubygems'
2require 'active_record'
3
4ddl = <<DDL
5create table if not exists rubyists (
6 id integer not null primary key
7 , name varchar(100) not null
8 , city text not null
9);
10DDL
11
12ActiveRecord::Base.establish_connection(
13 :adapter => "sqlite3" ,
14 :host => "localhost",
15 :database => ":memory:" ,
16 :username => "fake"
17)
18connection = ActiveRecord::Base.connection
19puts 'Class: ' + connection.class.to_s
20puts 'Is connected? ' + ActiveRecord::Base.connected?.to_s
21connection.execute( ddl )
22
23class Rubyist < ActiveRecord::Base
24end
25
26Rubyist.create( :name => 'Mitali Talim', :city => "Nashville, Tennessee" )
27Rubyist.create( :name => 'Sunil Kelkar', :city => "Pune, India" )
28Rubyist.create( :name => 'Adam Smith' , :city => "SanFransisco, USA" )
29
30participant = Rubyist.find( :first )
31puts %{ #{ participant.name } stays in #{ participant.city } }
32
33Rubyist.find( :first ).destroy
34
35participant = Rubyist.find( :first )
36puts %{ #{ participant.name } stays in #{ participant.city } }
37
38connection.disconnect!()
39puts 'Is connected? ' + ActiveRecord::Base.connected?.to_s
40
41participant = Rubyist.find( :first )
42puts %{ #{ participant.name } stays in #{ participant.city } }