The Depot Application

The Depot Application

20 ActiveRecord: Object Life Cycle 18 Active Record: The Basics

19 ActiveRecord: Relationships Between Tables

irb e1/ar/associations.rb
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/will_paginate-3.0.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/test-unit-2.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/sqlite3-1.3.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/ext", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/minitest-3.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/jquery-rails-0.2.7/lib", "/home/rubys/git/rails/lib", "/home/rubys/git/rails/railties/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rdoc-3.12/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/json-1.7.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/htmlentities-4.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/haml-3.1.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/capistrano-2.12.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-gateway-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-sftp-2.0.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-scp-1.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-2.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/highline-1.6.13/lib", "/home/rubys/git/rails/activeresource/lib", "/home/rubys/git/rails/activerecord/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/activemerchant-1.10.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/braintree-2.16.0/lib", "/home/rubys/git/rails/actionmailer/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mail-2.2.19/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/treetop-1.4.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/polyglot-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mime-types-1.19/lib", "/home/rubys/git/rails/actionpack/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/tzinfo-0.3.33/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-test-0.5.7/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/erubis-2.6.6/lib", "/home/rubys/git/rails/activemodel/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/i18n-0.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/builder-2.1.2/lib", "/home/rubys/git/rails/activesupport/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/bundler-1.1.3/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/x86_64-linux", ".", "/home/rubys/git/awdwr/edition3/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> false
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> false
>> require "active_record"
=> false
 
>> ActiveRecord::Schema.define do
 
?>   create_table :products, :force => true do |t|
?>     t.string   :title
>>     t.text     :description
>>     t.string   :image_url
>>     t.decimal  :price, :precision => 10, :scale => 2
>>     t.datetime :available_at
>>   end
 
>>   create_table :line_items, :force => true do |t|
?>     t.integer :product_id
>>     t.integer :order_id
>>     t.integer :quantity
>>     t.integer :unit_price, :precision => 8, :scale => 2
>>   end
>> end
-- create_table(:products, {:force=>true})
   -> 0.2847s
-- create_table(:line_items, {:force=>true})
   -> 0.1987s
=> nil
 
?> class Product < ActiveRecord::Base
>>   has_many :line_items
>> end
=> [Product(id: integer, title: string, description: text, image_url: string, price: decimal, available_at: datetime)]
 
>> class LineItem < ActiveRecord::Base
>>   belongs_to :product
>> end
=> nil
 
>> LineItem.delete_all
=> 0
 
>> p = Product.create(:title => "Programming Ruby", :available_at => Time.now)
=> #<Product id: 1, title: "Programming Ruby", description: nil, image_url: nil, price: nil, available_at: "2012-06-29 19:19:55">
>> l = LineItem.new
=> #<LineItem id: nil, product_id: nil, order_id: nil, quantity: nil, unit_price: nil>
>> l.id = 2
=> 2
>> l.product = p
=> #<Product id: 1, title: "Programming Ruby", description: nil, image_url: nil, price: nil, available_at: "2012-06-29 19:19:55">
>> l.save!
=> true
 
>> l = LineItem.new
=> #<LineItem id: nil, product_id: nil, order_id: nil, quantity: nil, unit_price: nil>
>> l.product = p
=> #<Product id: 1, title: "Programming Ruby", description: nil, image_url: nil, price: nil, available_at: "2012-06-29 19:19:55">
>> l.save!
=> true
 
>> puts "\n\nSimple Belongs to"

    

    
Simple Belongs to
=> nil
 
?> item = LineItem.find(2)
=> #<LineItem id: 2, product_id: 1, order_id: nil, quantity: nil, unit_price: nil>
 
>> # item.product is the associated Product object
?> puts "Current product is #{item.product.id}"
Current product is 1
=> nil
>> puts item.product.title
Programming Ruby
=> nil
 
>> item.product = Product.new(:title          => "Rails for Java Developers",
?>                            :description    => "...",
?>                            :image_url      => "http://....jpg",
?>                            :price          => 34.95,
?>                            :available_at   => Time.now)
=> #<Product id: nil, title: "Rails for Java Developers", description: "...", image_url: "http://....jpg", price: #<BigDecimal:7f06da656d40,'0.3495E2',18(18)>, available_at: "2012-06-29 19:19:56">
>> item.save!
=> true
 
>> puts "New product is #{item.product.id}"
New product is 2
=> nil
>> puts item.product.title
Rails for Java Developers
=> nil
 
>> puts "\n\nCreate belongs to"

    

    
Create belongs to
=> nil
 
>> item = LineItem.find(2)
=> #<LineItem id: 2, product_id: 2, order_id: nil, quantity: nil, unit_price: nil>
 
>> # item.product is the associated Product object
?> puts "Current product is #{item.product.id}"
Current product is 2
=> nil
>> puts item.product.title
Rails for Java Developers
=> nil
 
?> item.create_product(:title          => "Rails Recipes",
?>                     :description    => "...",
?>                     :image_url      => "http://....jpg",
?>                     :price          => 32.95,
?>                     :available_at   => Time.now)
=> #<Product id: 3, title: "Rails Recipes", description: "...", image_url: "http://....jpg", price: #<BigDecimal:7f06da5f1580,'0.3295E2',18(18)>, available_at: "2012-06-29 19:19:56">
 
>> puts "New product is #{item.product.id}"
New product is 3
=> nil
>> puts item.product.title
Rails Recipes
=> nil
 
>> puts "\n\nproduct belongs to"

    

    
product belongs to
=> nil
>> item = LineItem.find(2)
=> #<LineItem id: 2, product_id: 2, order_id: nil, quantity: nil, unit_price: nil>
 
>> # item.product is the associated Product object
?> puts "Current product is #{item.product.id}"
Current product is 2
=> nil
>> puts item.product.title
Rails for Java Developers
=> nil
 
>> item = LineItem.new()
=> #<LineItem id: nil, product_id: nil, order_id: nil, quantity: nil, unit_price: nil>
 
>> item.create_product(:title          => "Advanced Rails", 
?>                     :description    => "...",
?>                     :image_url      => "http://....jpg",
?>                     :price          => 34.95,
?>                     :available_at   => Time.now)
=> #<Product id: 4, title: "Advanced Rails", description: "...", image_url: "http://....jpg", price: #<BigDecimal:7f06da598de0,'0.3495E2',18(18)>, available_at: "2012-06-29 19:19:56">
 
>> puts "New product is #{item.product.id}"
New product is 4
=> nil
>> puts item.product.title
Advanced Rails
=> nil
>> item.save!
=> true
>> p item.product(true)
#<Product id: 4, title: "Advanced Rails", description: "...", image_url: "http://....jpg", price: #<BigDecimal:7f06da583648,'0.3495E2',18(18)>, available_at: "2012-06-29 19:19:56">
=> nil
 
>> puts "============="
=============
=> nil
 
>> prod = Product.find(item.product_id)
=> #<Product id: 4, title: "Advanced Rails", description: "...", image_url: "http://....jpg", price: #<BigDecimal:7f06da578040,'0.3495E2',18(18)>, available_at: "2012-06-29 19:19:56">
>> p prod.line_items.size
1
=> nil
 
irb e1/ar/sti.rb
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/will_paginate-3.0.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/test-unit-2.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/sqlite3-1.3.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/ext", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/minitest-3.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/jquery-rails-0.2.7/lib", "/home/rubys/git/rails/lib", "/home/rubys/git/rails/railties/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rdoc-3.12/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/json-1.7.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/htmlentities-4.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/haml-3.1.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/capistrano-2.12.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-gateway-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-sftp-2.0.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-scp-1.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-2.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/highline-1.6.13/lib", "/home/rubys/git/rails/activeresource/lib", "/home/rubys/git/rails/activerecord/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/activemerchant-1.10.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/braintree-2.16.0/lib", "/home/rubys/git/rails/actionmailer/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mail-2.2.19/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/treetop-1.4.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/polyglot-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mime-types-1.19/lib", "/home/rubys/git/rails/actionpack/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/tzinfo-0.3.33/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-test-0.5.7/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/erubis-2.6.6/lib", "/home/rubys/git/rails/activemodel/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/i18n-0.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/builder-2.1.2/lib", "/home/rubys/git/rails/activesupport/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/bundler-1.1.3/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/x86_64-linux", ".", "/home/rubys/git/awdwr/edition3/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> false
 
>> ActiveRecord::Base.logger = Logger.new(STDERR)
=> #<Logger:0x7fa6c76cfeb0 @level=0, @default_formatter=#<Logger::Formatter:0x7fa6c76cfe38 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x7fa6c76cfc80 @filename=nil, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x7fa6c76cfc30 @mon_owner=nil, @mon_waiting_queue=[], @mon_entering_queue=[], @mon_count=0>, @dev=#<IO:0x7fa6cd95aaa8>, @shift_size=nil, @shift_age=nil>, @formatter=nil, @progname=nil>
 
>> ActiveRecord::Schema.define do
 
?>   create_table :people, :force => true do |t|
?>     t.string :type
 
>>     # common attributes
?>     t.string :name
>>     t.string :email
 
>>     # attributes for type=Customer
?>     t.decimal :balance, :precision => 10, :scale => 2
 
>>     # attributes for type=Employee
?>     t.integer :reports_to
>>     t.integer :dept
 
>>     # attributes for type=Manager
?>     # -- none -- 
?>   end
 
>> end
-- create_table(:people, {:force=>true})
  *[1m*[36mSQL (0.3ms)  *[1mselect sqlite_version(*)
  *[1m*[35mSQL (0.8ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'

    
  *[1m*[36mSQL (159.0ms)  *[1mCREATE TABLE "people" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "type" varchar(255), "name" varchar(255), "email" varchar(255), "balance" decimal(10,2), "reports_to" integer, "dept" integer) 
   -> 0.1725s
=> nil
 
?> class Person < ActiveRecord::Base
>> end
=> nil
 
>> class Customer < Person
>> end
=> nil
 
>> class Employee < Person
>>   belongs_to :boss, :class_name => "Manager", :foreign_key => :reports_to
>> end
=> nil
 
>> class Manager < Employee
>> end
=> nil
 
?> Customer.create(:name => 'John Doe',    :email => "john@doe.com",    
?>                 :balance => 78.29)
  *[1m*[35mSQL (0.7ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'

    
  *[1m*[36mAREL (0.5ms)  *[1mINSERT INTO "people" ("name", "email", "reports_to", "type", "dept", "balance") VALUES ('John Doe', 'john@doe.com', NULL, 'Customer', NULL, 78.29)
=> #<Customer id: 1, type: "Customer", name: "John Doe", email: "john@doe.com", balance: #<BigDecimal:7fa6c74a2b60,'0.7829E2',18(18)>, reports_to: nil, dept: nil>
 
>> wilma = Manager.create(:name  => 'Wilma Flint', :email => "wilma@here.com",  
?>                        :dept => 23)
  *[1m*[35mAREL (0.5ms)  INSERT INTO "people" ("dept", "balance", "reports_to", "email", "name", "type") VALUES (23, NULL, NULL, 'wilma@here.com', 'Wilma Flint', 'Manager')
=> #<Manager id: 2, type: "Manager", name: "Wilma Flint", email: "wilma@here.com", balance: nil, reports_to: nil, dept: 23>
 
>> Customer.create(:name => 'Bert Public', :email => "b@public.net",    
?>                 :balance => 12.45)
  *[1m*[36mAREL (0.5ms)  *[1mINSERT INTO "people" ("name", "email", "reports_to", "type", "dept", "balance") VALUES ('Bert Public', 'b@public.net', NULL, 'Customer', NULL, 12.45)
=> #<Customer id: 3, type: "Customer", name: "Bert Public", email: "b@public.net", balance: #<BigDecimal:7fa6c7438e90,'0.1245E2',18(18)>, reports_to: nil, dept: nil>
 
>> barney = Employee.new(:name => 'Barney Rub',  :email => "barney@here.com", 
?>                       :dept => 23)
=> #<Employee id: nil, type: "Employee", name: "Barney Rub", email: "barney@here.com", balance: nil, reports_to: nil, dept: 23>
>> barney.boss = wilma
=> #<Manager id: 2, type: "Manager", name: "Wilma Flint", email: "wilma@here.com", balance: nil, reports_to: nil, dept: 23>
>> barney.save!
  *[1m*[35mAREL (0.5ms)  INSERT INTO "people" ("dept", "name", "reports_to", "email", "balance", "type") VALUES (23, 'Barney Rub', 2, 'barney@here.com', NULL, 'Employee')
=> true
 
>> manager = Person.find_by_name("Wilma Flint")
  *[1m*[36mPerson Load (0.5ms)  *[1mSELECT "people".* FROM "people" WHERE "people"."name" = 'Wilma Flint' LIMIT 1
=> #<Manager id: 2, type: "Manager", name: "Wilma Flint", email: "wilma@here.com", balance: nil, reports_to: nil, dept: 23>
>> puts manager.class    #=> Manager
Manager
=> nil
>> puts manager.email    #=> wilma@here.com
wilma@here.com
=> nil
>> puts manager.dept     #=> 23
23
=> nil
 
>> customer = Person.find_by_name("Bert Public")
  *[1m*[35mPerson Load (0.5ms)  SELECT "people".* FROM "people" WHERE "people"."name" = 'Bert Public' LIMIT 1
=> #<Customer id: 3, type: "Customer", name: "Bert Public", email: "b@public.net", balance: #<BigDecimal:7fa6c73b41e0,'0.1245E2',18(18)>, reports_to: nil, dept: nil>
>> puts customer.class    #=> Customer
Customer
=> nil
>> puts customer.email    #=> b@public.net
b@public.net
=> nil
>> puts customer.balance  #=> 12.45
12.45
=> nil
 
>> b = Person.find_by_name("Barney Rub")
  *[1m*[36mPerson Load (0.5ms)  *[1mSELECT "people".* FROM "people" WHERE "people"."name" = 'Barney Rub' LIMIT 1
=> #<Employee id: 4, type: "Employee", name: "Barney Rub", email: "barney@here.com", balance: nil, reports_to: 2, dept: 23>
  *[1m*[35mManager Load (0.5ms)  SELECT "people".* FROM "people" WHERE "people"."type" = 'Manager' AND "people"."id" = 2 LIMIT 1
>> p b.boss
#<Manager id: 2, type: "Manager", name: "Wilma Flint", email: "wilma@here.com", balance: nil, reports_to: nil, dept: 23>
=> nil
 
irb e1/ar/polymorphic.rb 1
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/will_paginate-3.0.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/test-unit-2.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/sqlite3-1.3.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/ext", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/minitest-3.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/jquery-rails-0.2.7/lib", "/home/rubys/git/rails/lib", "/home/rubys/git/rails/railties/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rdoc-3.12/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/json-1.7.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/htmlentities-4.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/haml-3.1.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/capistrano-2.12.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-gateway-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-sftp-2.0.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-scp-1.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-2.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/highline-1.6.13/lib", "/home/rubys/git/rails/activeresource/lib", "/home/rubys/git/rails/activerecord/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/activemerchant-1.10.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/braintree-2.16.0/lib", "/home/rubys/git/rails/actionmailer/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mail-2.2.19/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/treetop-1.4.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/polyglot-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mime-types-1.19/lib", "/home/rubys/git/rails/actionpack/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/tzinfo-0.3.33/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-test-0.5.7/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/erubis-2.6.6/lib", "/home/rubys/git/rails/activemodel/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/i18n-0.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/builder-2.1.2/lib", "/home/rubys/git/rails/activesupport/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/bundler-1.1.3/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/x86_64-linux", ".", "/home/rubys/git/awdwr/edition3/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> false
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> false
>> require "active_record"
=> false
 
>> if ARGV.empty? or ARGV.first == '1'
>>   ActiveRecord::Schema.define do
 
?>     create_table :catalog_entries, :force => true do |t|
?>       t.string :name
>>       t.datetime :acquired_at
>>       t.integer :resource_id
>>       t.string :resource_type
>>     end
 
?>     create_table :articles, :force => true do |t|
?>       t.text :content
>>     end
 
>>     create_table :sounds, :force => true do |t|
?>       t.binary :content
>>     end
 
>>     create_table :images, :force => true do |t|
?>       t.binary :content
>>     end
 
>>   end
>> end
-- create_table(:catalog_entries, {:force=>true})
   -> 0.1868s
-- create_table(:articles, {:force=>true})
   -> 0.0900s
-- create_table(:sounds, {:force=>true})
   -> 0.1179s
-- create_table(:images, {:force=>true})
   -> 0.1540s
=> nil
 
?> class CatalogEntry < ActiveRecord::Base
>>   belongs_to :resource, :polymorphic => true
>> end
=> nil
 
?> class Article < ActiveRecord::Base
>>   has_one :catalog_entry, :as => :resource
>> end
=> nil
 
>> class Sound < ActiveRecord::Base
>>   has_one :catalog_entry, :as => :resource
>> end
=> nil
 
>> class Image < ActiveRecord::Base
>>   has_one :catalog_entry, :as => :resource
>> end
=> nil
 
>> case ARGV.shift
>> when "1"
>>   a = Article.new(:content => "This is my new article")
>>   c = CatalogEntry.new(:name => 'Article One', :acquired_at => Time.now)
>>   c.resource = a
>>   c.save!
?>   article = Article.find(1)
>>   p article.catalog_entry.name  #=> "Article One"
 
>>   cat = CatalogEntry.find(1)
>>   resource = cat.resource 
>>   p resource                    #=> #<Article:0x640d80 @attributes={"id"=>"1", 
>>                                 #     "content"=>"This is my new article"}>
 
?> when "2"
>>   c = CatalogEntry.new(:name => 'Article One', :acquired_at => Time.now)
>>   c.resource = Article.new(:content => "This is my new article")
>>   c.save!
 
>>   c = CatalogEntry.new(:name => 'Image One', :acquired_at => Time.now)
>>   c.resource = Image.new(:content => "some binary data")
>>   c.save!
 
>>   c = CatalogEntry.new(:name => 'Sound One', :acquired_at => Time.now)
>>   c.resource = Sound.new(:content => "more binary data")
>>   c.save!
 
?>   CatalogEntry.find(:all).each do |c|
?>     puts "#{c.name}:  #{c.resource.class}"
>>   end
>> else
?>   a = Sound.new(:content => "ding!")
>>   c = CatalogEntry.new(:name => 'Sound One', :acquired_at => Time.now)
>>   c.resource = a
 
>>   c.save!
 
>>   c = CatalogEntry.find 1
>>   p c.resource
 
>>   a = Sound.find :first
>>   p a.catalog_entry
>> end
"Article One"
#<Article id: 1, content: "This is my new article">
=> nil
 
sqlite3> select * from articles
     id = 1
content = This is my new article
sqlite3> select * from catalog_entries
           id = 1
         name = Article One
  acquired_at = 2012-06-29 19:20:07.218355
  resource_id = 1
resource_type = Article
sqlite3> delete from catalog_entries
irb e1/ar/polymorphic.rb 2
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/will_paginate-3.0.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/test-unit-2.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/sqlite3-1.3.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/ext", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/minitest-3.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/jquery-rails-0.2.7/lib", "/home/rubys/git/rails/lib", "/home/rubys/git/rails/railties/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rdoc-3.12/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/json-1.7.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/htmlentities-4.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/haml-3.1.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/capistrano-2.12.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-gateway-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-sftp-2.0.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-scp-1.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-2.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/highline-1.6.13/lib", "/home/rubys/git/rails/activeresource/lib", "/home/rubys/git/rails/activerecord/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/activemerchant-1.10.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/braintree-2.16.0/lib", "/home/rubys/git/rails/actionmailer/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mail-2.2.19/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/treetop-1.4.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/polyglot-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mime-types-1.19/lib", "/home/rubys/git/rails/actionpack/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/tzinfo-0.3.33/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-test-0.5.7/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/erubis-2.6.6/lib", "/home/rubys/git/rails/activemodel/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/i18n-0.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/builder-2.1.2/lib", "/home/rubys/git/rails/activesupport/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/bundler-1.1.3/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/x86_64-linux", ".", "/home/rubys/git/awdwr/edition3/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> false
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> false
>> require "active_record"
=> false
 
>> if ARGV.empty? or ARGV.first == '1'
>>   ActiveRecord::Schema.define do
 
?>     create_table :catalog_entries, :force => true do |t|
?>       t.string :name
>>       t.datetime :acquired_at
>>       t.integer :resource_id
>>       t.string :resource_type
>>     end
 
?>     create_table :articles, :force => true do |t|
?>       t.text :content
>>     end
 
>>     create_table :sounds, :force => true do |t|
?>       t.binary :content
>>     end
 
>>     create_table :images, :force => true do |t|
?>       t.binary :content
>>     end
 
>>   end
>> end
=> nil
 
?> class CatalogEntry < ActiveRecord::Base
>>   belongs_to :resource, :polymorphic => true
>> end
=> nil
 
?> class Article < ActiveRecord::Base
>>   has_one :catalog_entry, :as => :resource
>> end
=> nil
 
>> class Sound < ActiveRecord::Base
>>   has_one :catalog_entry, :as => :resource
>> end
=> nil
 
>> class Image < ActiveRecord::Base
>>   has_one :catalog_entry, :as => :resource
>> end
=> nil
 
>> case ARGV.shift
>> when "1"
>>   a = Article.new(:content => "This is my new article")
>>   c = CatalogEntry.new(:name => 'Article One', :acquired_at => Time.now)
>>   c.resource = a
>>   c.save!
?>   article = Article.find(1)
>>   p article.catalog_entry.name  #=> "Article One"
 
>>   cat = CatalogEntry.find(1)
>>   resource = cat.resource 
>>   p resource                    #=> #<Article:0x640d80 @attributes={"id"=>"1", 
>>                                 #     "content"=>"This is my new article"}>
 
?> when "2"
>>   c = CatalogEntry.new(:name => 'Article One', :acquired_at => Time.now)
>>   c.resource = Article.new(:content => "This is my new article")
>>   c.save!
 
>>   c = CatalogEntry.new(:name => 'Image One', :acquired_at => Time.now)
>>   c.resource = Image.new(:content => "some binary data")
>>   c.save!
 
>>   c = CatalogEntry.new(:name => 'Sound One', :acquired_at => Time.now)
>>   c.resource = Sound.new(:content => "more binary data")
>>   c.save!
 
?>   CatalogEntry.find(:all).each do |c|
?>     puts "#{c.name}:  #{c.resource.class}"
>>   end
>> else
?>   a = Sound.new(:content => "ding!")
>>   c = CatalogEntry.new(:name => 'Sound One', :acquired_at => Time.now)
>>   c.resource = a
 
>>   c.save!
 
>>   c = CatalogEntry.find 1
>>   p c.resource
 
>>   a = Sound.find :first
>>   p a.catalog_entry
>> end
Article One:  Article
Image One:  Image
Sound One:  Sound
=> [#<CatalogEntry id: 2, name: "Article One", acquired_at: "2012-06-29 19:20:12", resource_id: 2, resource_type: "Article">, #<CatalogEntry id: 3, name: "Image One", acquired_at: "2012-06-29 19:20:12", resource_id: 1, resource_type: "Image">, #<CatalogEntry id: 4, name: "Sound One", acquired_at: "2012-06-29 19:20:12", resource_id: 1, resource_type: "Sound">]
 
sqlite3> select * from articles
     id = 1
content = This is my new article
 
     id = 2
content = This is my new article
sqlite3> select * from images
     id = 1
content = some binary data
sqlite3> select * from sounds
     id = 1
content = more binary data
sqlite3> select * from catalog_entries
           id = 2
         name = Article One
  acquired_at = 2012-06-29 19:20:12.157933
  resource_id = 2
resource_type = Article
 
           id = 3
         name = Image One
  acquired_at = 2012-06-29 19:20:12.360517
  resource_id = 1
resource_type = Image
 
           id = 4
         name = Sound One
  acquired_at = 2012-06-29 19:20:12.485329
  resource_id = 1
resource_type = Sound
irb e1/ar/self_association.rb
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/will_paginate-3.0.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/test-unit-2.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/sqlite3-1.3.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/ext", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/minitest-3.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/jquery-rails-0.2.7/lib", "/home/rubys/git/rails/lib", "/home/rubys/git/rails/railties/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rdoc-3.12/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/json-1.7.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/htmlentities-4.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/haml-3.1.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/capistrano-2.12.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-gateway-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-sftp-2.0.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-scp-1.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-2.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/highline-1.6.13/lib", "/home/rubys/git/rails/activeresource/lib", "/home/rubys/git/rails/activerecord/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/activemerchant-1.10.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/braintree-2.16.0/lib", "/home/rubys/git/rails/actionmailer/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mail-2.2.19/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/treetop-1.4.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/polyglot-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mime-types-1.19/lib", "/home/rubys/git/rails/actionpack/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/tzinfo-0.3.33/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-test-0.5.7/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/erubis-2.6.6/lib", "/home/rubys/git/rails/activemodel/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/i18n-0.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/builder-2.1.2/lib", "/home/rubys/git/rails/activesupport/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/bundler-1.1.3/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/x86_64-linux", ".", "/home/rubys/git/awdwr/edition3/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> false
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> false
>> require "active_record"
=> false
 
>> ActiveRecord::Schema.define do
?>   create_table :employees, :force => true do |t|
?>     t.string :name
>>     t.integer :manager_id
>>     t.integer :mentor_id
>>   end
>> end
-- create_table(:employees, {:force=>true})
   -> 0.1969s
=> nil
 
?> class Employee < ActiveRecord::Base
>>   belongs_to :manager,
?>              :class_name  => "Employee",
?>              :foreign_key => "manager_id"
 
>>   belongs_to :mentor,
?>              :class_name  => "Employee", 
?>              :foreign_key => "mentor_id"
 
>>   has_many   :mentored_employees,
?>              :class_name  => "Employee",
?>              :foreign_key => "mentor_id"
 
>>   has_many   :managed_employees,
?>              :class_name  => "Employee",
?>              :foreign_key => "manager_id"
>> end
=> [Employee(id: integer, name: string, manager_id: integer, mentor_id: integer)]
 
?> Employee.delete_all
=> 0
 
>> adam = Employee.create(:name => "Adam")
=> #<Employee id: 1, name: "Adam", manager_id: nil, mentor_id: nil>
>> beth = Employee.create(:name => "Beth")
=> #<Employee id: 2, name: "Beth", manager_id: nil, mentor_id: nil>
 
>> clem = Employee.new(:name => "Clem")
=> #<Employee id: nil, name: "Clem", manager_id: nil, mentor_id: nil>
>> clem.manager = adam
=> #<Employee id: 1, name: "Adam", manager_id: nil, mentor_id: nil>
>> clem.mentor  = beth
=> #<Employee id: 2, name: "Beth", manager_id: nil, mentor_id: nil>
>> clem.save!
=> true
 
>> dawn = Employee.new(:name => "Dawn")
=> #<Employee id: nil, name: "Dawn", manager_id: nil, mentor_id: nil>
>> dawn.manager = adam
=> #<Employee id: 1, name: "Adam", manager_id: nil, mentor_id: nil>
>> dawn.mentor  = clem
=> #<Employee id: 3, name: "Clem", manager_id: 1, mentor_id: 2>
>> dawn.save!
=> true
 
?> p adam.managed_employees.map {|e| e.name}  # => [ "Clem", "Dawn" ]
["Clem", "Dawn"]
=> nil
>> p adam.mentored_employees                  # => []
[]
=> nil
>> p dawn.mentor.name                         # => "Clem"
"Clem"
=> nil
 
 
>> >> => nil
irb e1/ar/acts_as_list.rb
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/will_paginate-3.0.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/test-unit-2.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/sqlite3-1.3.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/ext", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/minitest-3.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/jquery-rails-0.2.7/lib", "/home/rubys/git/rails/lib", "/home/rubys/git/rails/railties/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rdoc-3.12/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/json-1.7.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/htmlentities-4.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/haml-3.1.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/capistrano-2.12.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-gateway-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-sftp-2.0.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-scp-1.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-2.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/highline-1.6.13/lib", "/home/rubys/git/rails/activeresource/lib", "/home/rubys/git/rails/activerecord/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/activemerchant-1.10.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/braintree-2.16.0/lib", "/home/rubys/git/rails/actionmailer/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mail-2.2.19/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/treetop-1.4.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/polyglot-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mime-types-1.19/lib", "/home/rubys/git/rails/actionpack/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/tzinfo-0.3.33/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-test-0.5.7/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/erubis-2.6.6/lib", "/home/rubys/git/rails/activemodel/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/i18n-0.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/builder-2.1.2/lib", "/home/rubys/git/rails/activesupport/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/bundler-1.1.3/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/x86_64-linux", ".", "/home/rubys/git/awdwr/edition3/data/code/e1/ar"]
>> require "connect"
=> true
 
>> require "logger"
=> false
>> require "rubygems"
=> false
>> require "active_record"
=> false
 
>> require "./vendor/plugins/acts_as_list/init"
=> true
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> ActiveRecord::Base.connection.instance_eval do
 
?>   create_table :parents, :force => true do |t|
?>   end
 
>>   create_table :children, :force => true do |t|
?>     t.integer :parent_id
>>     t.string  :name
>>     t.integer :position
>>   end
>> end
=> []
 
 
?> class Parent < ActiveRecord::Base
>>   has_many :children, :order => :position
>> end
=> [Parent(id: integer)]
 
>> class Child < ActiveRecord::Base
>>   belongs_to :parent
>>   acts_as_list  :scope => :parent
>> end
=> [Child(id: integer, parent_id: integer, name: string, position: integer)]
 
 
?> parent = Parent.create
=> #<Parent id: 1>
>> %w{ One Two Three Four}.each do |name|
?>   parent.children.create(:name => name)
>> end
=> ["One", "Two", "Three", "Four"]
>> parent.save
=> true
 
?> def display_children(parent)
>>   puts parent.children(true).map {|child| child.name }.join(", ")
>> end
=> nil
 
?> display_children(parent)         #=> One, Two, Three, Four
One, Two, Three, Four
=> nil
 
>> puts parent.children[0].first?   #=> true
true
=> nil
 
>> two = parent.children[1]
=> #<Child id: 2, parent_id: 1, name: "Two", position: 2>
>> puts two.lower_item.name         #=> Three
Three
=> nil
>> puts two.higher_item.name        #=> One
One
=> nil
 
>> parent.children[0].move_lower
=> true
>> display_children(parent)         #=> Two, One, Three, Four
Two, One, Three, Four
=> nil
 
>> parent.children[2].move_to_top
=> true
>> display_children(parent)         #=> Three, Two, One, Four
Three, Two, One, Four
=> nil
 
>> parent.children[2].destroy
=> #<Child id: 1, parent_id: 1, name: "One", position: nil>
>> display_children(parent)         #=> Three, Two, Four
Three, Two, Four
=> nil
>> >> => nil
irb e1/ar/acts_as_tree.rb
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/will_paginate-3.0.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/test-unit-2.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/sqlite3-1.3.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/ext", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/minitest-3.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/jquery-rails-0.2.7/lib", "/home/rubys/git/rails/lib", "/home/rubys/git/rails/railties/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rdoc-3.12/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/json-1.7.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/htmlentities-4.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/haml-3.1.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/capistrano-2.12.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-gateway-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-sftp-2.0.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-scp-1.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-2.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/highline-1.6.13/lib", "/home/rubys/git/rails/activeresource/lib", "/home/rubys/git/rails/activerecord/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/activemerchant-1.10.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/braintree-2.16.0/lib", "/home/rubys/git/rails/actionmailer/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mail-2.2.19/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/treetop-1.4.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/polyglot-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mime-types-1.19/lib", "/home/rubys/git/rails/actionpack/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/tzinfo-0.3.33/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-test-0.5.7/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/erubis-2.6.6/lib", "/home/rubys/git/rails/activemodel/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/i18n-0.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/builder-2.1.2/lib", "/home/rubys/git/rails/activesupport/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/bundler-1.1.3/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/x86_64-linux", ".", "/home/rubys/git/awdwr/edition3/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> false
>> require "rubygems"
=> false
>> require "active_record"
=> false
 
>> require "./vendor/plugins/acts_as_tree/lib/active_record/acts/tree.rb"
=> true
>> require "./vendor/plugins/acts_as_tree/init"
=> true
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> ActiveRecord::Schema.define do
 
?>   create_table :categories, :force => true do |t|
?>     t.string  :name
>>     t.integer :parent_id
>>   end
>> end
-- create_table(:categories, {:force=>true})
   -> 0.1886s
=> nil
 
 
 
 
?> class Category < ActiveRecord::Base
>>   acts_as_tree  :order => "name"
>> end
=> nil
 
 
?> root        = Category.create(:name => "Books")
=> #<Category id: 1, name: "Books", parent_id: nil>
>> fiction     = root.children.create(:name => "Fiction")
=> #<Category id: 2, name: "Fiction", parent_id: 1>
>> non_fiction = root.children.create(:name => "Non Fiction")
=> #<Category id: 3, name: "Non Fiction", parent_id: 1>
 
>> non_fiction.children.create(:name => "Computers")
=> #<Category id: 4, name: "Computers", parent_id: 3>
>> non_fiction.children.create(:name => "Science")
=> #<Category id: 5, name: "Science", parent_id: 3>
>> non_fiction.children.create(:name => "Art History")
=> #<Category id: 6, name: "Art History", parent_id: 3>
 
>> fiction.children.create(:name => "Mystery")
=> #<Category id: 7, name: "Mystery", parent_id: 2>
>> fiction.children.create(:name => "Romance")
=> #<Category id: 8, name: "Romance", parent_id: 2>
>> fiction.children.create(:name => "Science Fiction")
=> #<Category id: 9, name: "Science Fiction", parent_id: 2>
 
>> def display_children(order)
>>   puts order.children.map {|child| child.name }.join(", ")
>> end
=> nil
 
?> display_children(root)             # Fiction, Non Fiction
Fiction, Non Fiction
=> nil
 
>> sub_category = root.children.first
=> #<Category id: 2, name: "Fiction", parent_id: 1>
>> puts sub_category.children.size    #=> 3
3
=> nil
>> display_children(sub_category)     #=> Mystery, Romance, Science Fiction
Mystery, Romance, Science Fiction
=> nil
 
>> non_fiction = root.children.find(:first, :conditions => "name = 'Non Fiction'")
=> #<Category id: 3, name: "Non Fiction", parent_id: 1>
 
>> display_children(non_fiction)      #=> Art History, Computers, Science
Art History, Computers, Science
=> nil
>> puts non_fiction.parent.name       #=> Books
Books
=> nil
>> >> => nil
irb e1/ar/one_to_one.rb
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/will_paginate-3.0.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/test-unit-2.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/sqlite3-1.3.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/ext", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/minitest-3.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/jquery-rails-0.2.7/lib", "/home/rubys/git/rails/lib", "/home/rubys/git/rails/railties/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rdoc-3.12/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/json-1.7.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/htmlentities-4.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/haml-3.1.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/capistrano-2.12.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-gateway-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-sftp-2.0.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-scp-1.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-2.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/highline-1.6.13/lib", "/home/rubys/git/rails/activeresource/lib", "/home/rubys/git/rails/activerecord/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/activemerchant-1.10.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/braintree-2.16.0/lib", "/home/rubys/git/rails/actionmailer/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mail-2.2.19/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/treetop-1.4.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/polyglot-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mime-types-1.19/lib", "/home/rubys/git/rails/actionpack/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/tzinfo-0.3.33/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-test-0.5.7/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/erubis-2.6.6/lib", "/home/rubys/git/rails/activemodel/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/i18n-0.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/builder-2.1.2/lib", "/home/rubys/git/rails/activesupport/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/bundler-1.1.3/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/x86_64-linux", ".", "/home/rubys/git/awdwr/edition3/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> false
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> false
>> require "active_record"
=> false
 
>> ActiveRecord::Schema.define do
 
?>   create_table :invoices, :force => true do |t|
?>     t.integer :order_id
>>   end
 
>>   create_table :orders, :force => true do |t|
?>     t.string   :name
>>     t.string   :email
>>     t.text     :address
>>     t.string   :pay_type
>>     t.datetime :shipped_at
>>   end
 
>> end
-- create_table(:invoices, {:force=>true})
   -> 0.1799s
-- create_table(:orders, {:force=>true})
   -> 0.2153s
=> nil
 
?> class Order < ActiveRecord::Base
>>   has_one :invoice
>> end
=> nil
 
>> class Invoice < ActiveRecord::Base
>>   belongs_to :order
>> end
=> nil
 
>> Order.create(:name => "Dave", :email => "dave@xxx",
?>              :address => "123 Main St", :pay_type => "credit",
?>              :shipped_at => Time.now)
=> #<Order id: 1, name: "Dave", email: "dave@xxx", address: "123 Main St", pay_type: "credit", shipped_at: "2012-06-29 19:20:34">
 
 
 
>> order = Order.find(1)
=> #<Order id: 1, name: "Dave", email: "dave@xxx", address: "123 Main St", pay_type: "credit", shipped_at: "2012-06-29 19:20:34">
 
>> p order.invoice
nil
=> nil
 
>> invoice = Invoice.new
=> #<Invoice id: nil, order_id: nil>
>> if invoice.save
>>   order.invoice = invoice
>> else
?>   fail invoice.errors.to_s
>> end
=> #<Invoice id: 1, order_id: 1>
 
>> p order.invoice
#<Invoice id: 1, order_id: 1>
=> nil
 
>> o = Order.new
=> #<Order id: nil, name: nil, email: nil, address: nil, pay_type: nil, shipped_at: nil>
>> p o.id
nil
=> nil
>> invoice.order = o
=> #<Order id: nil, name: nil, email: nil, address: nil, pay_type: nil, shipped_at: nil>
>> p o.id
nil
=> nil
>> invoice.save
=> true
>> p o.id
2
=> nil
 
irb e1/ar/counters.rb
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/will_paginate-3.0.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/test-unit-2.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/sqlite3-1.3.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mysql-2.8.1/ext", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/minitest-3.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/jquery-rails-0.2.7/lib", "/home/rubys/git/rails/lib", "/home/rubys/git/rails/railties/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/thor-0.14.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rdoc-3.12/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/json-1.7.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/htmlentities-4.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/haml-3.1.6/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/capistrano-2.12.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-gateway-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-sftp-2.0.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-scp-1.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/net-ssh-2.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/highline-1.6.13/lib", "/home/rubys/git/rails/activeresource/lib", "/home/rubys/git/rails/activerecord/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/arel-2.0.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/activemerchant-1.10.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/braintree-2.16.0/lib", "/home/rubys/git/rails/actionmailer/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mail-2.2.19/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/treetop-1.4.10/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/polyglot-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/mime-types-1.19/lib", "/home/rubys/git/rails/actionpack/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/tzinfo-0.3.33/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-test-0.5.7/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-mount-0.6.14/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rack-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/erubis-2.6.6/lib", "/home/rubys/git/rails/activemodel/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/i18n-0.5.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/builder-2.1.2/lib", "/home/rubys/git/rails/activesupport/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib", "/home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/bundler-1.1.3/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8", "/home/rubys/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/x86_64-linux", ".", "/home/rubys/git/awdwr/edition3/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> false
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> false
>> require "active_record"
=> false
 
 
>> ActiveRecord::Schema.define do
?>   create_table :products, :force => true do |t|
?>     t.string :title
>>     t.text :description
>>     # ...
?>     t.integer :line_items_count, :default => 0
>>   end
 
>>   create_table :line_items, :force => true do |t|
?>     t.integer :product_id
>>     t.integer :order_id
>>     t.integer :quantity
>>     t.decimal :unit_price, :precision => 8, :scale => 2
>>   end
 
>> end
-- create_table(:products, {:force=>true})
   -> 0.3371s
-- create_table(:line_items, {:force=>true})
   -> 0.2403s
=> nil
 
>> class Product < ActiveRecord::Base
>>   has_many :line_items
>> end
=> [Product(id: integer, title: string, description: text, line_items_count: integer)]
 
?> class LineItem < ActiveRecord::Base
>>   belongs_to :product, :counter_cache => true
>> end
=> nil
 
?> product = Product.create(:title => "Programming Ruby",
?>                          :description => " ... ")
=> #<Product id: 1, title: "Programming Ruby", description: " ... ", line_items_count: 0>
>> line_item = LineItem.new
=> #<LineItem id: nil, product_id: nil, order_id: nil, quantity: nil, unit_price: nil>
>> line_item.product = product
=> #<Product id: 1, title: "Programming Ruby", description: " ... ", line_items_count: 0>
>> line_item.save
=> true
>> puts "In memory size = #{product.line_items.size}"             #=> 0
In memory size = 0
=> nil
>> puts "Refreshed size = #{product.line_items(:refresh).size}"   #=> 1
Refreshed size = 1
=> nil
 
 
 
>> LineItem.delete_all
=> 1
>> Product.delete_all
=> 1
 
?> product = Product.create(:title => "Programming Ruby", 
?>                          :description => " ... ")
=> #<Product id: 2, title: "Programming Ruby", description: " ... ", line_items_count: 0>
>> product.line_items.create
=> #<LineItem id: 2, product_id: 2, order_id: nil, quantity: nil, unit_price: nil>
>> puts "In memory size = #{product.line_items.size}"             #=> 1
In memory size = 0
=> nil
>> puts "Refreshed size = #{product.line_items(:refresh).size}"   #=> 1
Refreshed size = 1
=> nil
 
 
 
>> >> => nil

20 ActiveRecord: Object Life Cycle 18 Active Record: The Basics