Ruby sample scripts - Active records
add-cities.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "root",
:password => "welcome"
)
class City < ActiveRecord::Base ; end
City.create(:name => "Chennai", :country => "India")
City.create(:name => "Sydney", :country => "Australia")
add-people.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "root",
:password => "welcome"
)
class Person < ActiveRecord::Base
belongs_to :city
has_and_belongs_to_many :book
end
class Book < ActiveRecord::Base
has_and_belongs_to_many :person
end
class City < ActiveRecord::Base
has_many :person
end
Person.create(:name => "John", :city =>
City.find(:first, :conditions =>
{ :name => "Sydney" }))
Book.create(:name => "Programming in Ruby", :isbn => "343434")
create-books.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "root",
:password => "welcome"
)
ActiveRecord::Base.connection.create_table :books do |t|
t.column :name, :string, :length=>32
t.column :isbn, :string, :length=>10
end
create-person.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "root",
:password => "welcome"
)
ActiveRecord::Base.connection.create_table :city do |t|
t.column :name, :string, :length=>32
t.column :country, :string
end
ActiveRecord::Base.connection.create_table :people do |t|
t.column :name, :string, :length => 32
t.references :city
end
entries.rb
require 'ActiveRecord'
class Entries < ActiveRecord::Base
end
entry = Entry.new
entry.name = "John"
entry.comments = "What a great Web site!"
entry.save
entry = Entry.find(1)
puts entry.name # output: "John"
find-cities.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "root",
:password => "welcome"
)
class City < ActiveRecord::Base ; end
my_city = City.find(:first, :conditions => {:name => "Chennai" })
print my_city.name
print my_city.country
people-books.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "devel",
:password => "welcome"
)
class Person < ActiveRecord::Base
belongs_to :city
end
class City < ActiveRecord::Base
has_many :person
end
Person.create(:name => "John", :city =>
City.find(:first, :conditions =>
{ :name => "Sydney" }))
simple.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "devel",
:password => "welcome"
)
class Rubyist < ActiveRecord::Base
end
Rubyist.create(:name => 'Luc Juggery', :city => "Nashville, Tenessee")
Rubyist.create(:name => 'Sunil Kelkar', :city => "Pune, India")
Rubyist.create(:name => 'Adam Smith', :city => "San Fransisco, USA")
participant = Rubyist.find(:first)
puts %{#{participant.name} stays in #{participant.city}}
#Rubyist.find(:first).destroy