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
Switch to inspect mode.
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/git/awdwr/work-192-236/migration/app/controllers/", "/home/rubys/git/awdwr/work-192-236/migration/app", "/home/rubys/git/awdwr/work-192-236/migration/app/models", "/home/rubys/git/awdwr/work-192-236/migration/app/controllers", "/home/rubys/git/awdwr/work-192-236/migration/app/helpers", "/home/rubys/git/awdwr/work-192-236/migration/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib/../builtin/rails_info/", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activesupport/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionpack/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activerecord/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionmailer/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activeresource/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/i18n-0.4.0.beta1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mail-2.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mime-types-1.16/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-mount-0.6.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-format-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-test-0.5.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/tzinfo-0.3.20/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-scp-1.0.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/highline-1.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-gateway-1.0.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/ext", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/arel-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/builder-2.1.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/htmlentities-4.2.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-2.0.22/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/polyglot-0.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/will_paginate-3.0.pre/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-sftp-2.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/activesupport-3.0.0.beta3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/lib", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1/i686-linux", "/home/rubys/git/awdwr/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> []
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> []
>> require "active_record"
=> []
 
>> 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.1365s
-- create_table(:line_items, {:force=>true})
   -> 0.0072s
=> nil
 
?> class Product < ActiveRecord::Base
>>   has_many :line_items
>> end
=> [#<ActiveSupport::Callbacks::Callback:0x8d795a8 @kind=:validate, @method=:validate_associated_records_for_line_items, @identifier=nil, @options={}>]
 
>> 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: "2010-05-24 10:33:24">
>> 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: "2010-05-24 10:33:24">
>> 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: "2010-05-24 10:33:24">
>> 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:87e4160,'0.3495E2',8(8)>, available_at: "2010-05-24 10:33:24">
>> 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:96b7bc4,'0.3295E2',8(8)>, available_at: "2010-05-24 10:33:24">
 
>> 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:971425c,'0.3495E2',8(8)>, available_at: "2010-05-24 10:33:24">
 
>> 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:973748c,'0.3495E2',8(8)>, available_at: "2010-05-24 10:33:24">
=> #<Product id: 4, title: "Advanced Rails", description: "...", image_url: "http://....jpg", price: #<BigDecimal:9736fc8,'0.3495E2',8(8)>, available_at: "2010-05-24 10:33:24">
 
>> puts "============="
=============
=> nil
 
>> prod = Product.find(item.product_id)
=> #<Product id: 4, title: "Advanced Rails", description: "...", image_url: "http://....jpg", price: #<BigDecimal:973ef98,'0.3495E2',8(8)>, available_at: "2010-05-24 10:33:24">
>> p prod.line_items.size
1
=> 1
 
irb e1/ar/sti.rb
Switch to inspect mode.
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/git/awdwr/work-192-236/migration/app/controllers/", "/home/rubys/git/awdwr/work-192-236/migration/app", "/home/rubys/git/awdwr/work-192-236/migration/app/models", "/home/rubys/git/awdwr/work-192-236/migration/app/controllers", "/home/rubys/git/awdwr/work-192-236/migration/app/helpers", "/home/rubys/git/awdwr/work-192-236/migration/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib/../builtin/rails_info/", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activesupport/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionpack/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activerecord/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionmailer/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activeresource/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/i18n-0.4.0.beta1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mail-2.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mime-types-1.16/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-mount-0.6.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-format-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-test-0.5.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/tzinfo-0.3.20/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-scp-1.0.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/highline-1.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-gateway-1.0.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/ext", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/arel-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/builder-2.1.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/htmlentities-4.2.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-2.0.22/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/polyglot-0.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/will_paginate-3.0.pre/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-sftp-2.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/activesupport-3.0.0.beta3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/lib", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1/i686-linux", "/home/rubys/git/awdwr/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> []
 
>> ActiveRecord::Base.logger = Logger.new(STDERR)
=> #<Logger:0x8bf00d8 @progname=nil, @level=0, @default_formatter=#<Logger::Formatter:0x8bf00c4 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x8bf0074 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDERR>>, @mutex=#<Logger::LogDevice::LogDeviceMutex:0x8bf004c @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x8bf0024>>>>
 
>> 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})
  SQL (0.4ms)   select sqlite_version(*)
  SQL (1.1ms)    SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'

    
  SQL (6.3ms)   CREATE 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.1370s
=> 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)
  Customer Create (0.3ms)   INSERT INTO "people" ("type", "name", "email", "balance", "reports_to", "dept") VALUES('Customer', 'John Doe', 'john@doe.com', 78.29, NULL, NULL)
=> #<Customer id: 1, type: "Customer", name: "John Doe", email: "john@doe.com", balance: #<BigDecimal:9619654,'0.7829E2',8(8)>, reports_to: nil, dept: nil>
 
>> wilma = Manager.create(:name  => 'Wilma Flint', :email => "wilma@here.com",  
?>                        :dept => 23)
  Manager Create (0.3ms)   INSERT INTO "people" ("type", "name", "email", "balance", "reports_to", "dept") VALUES('Manager', 'Wilma Flint', 'wilma@here.com', NULL, NULL, 23)
=> #<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)
  Customer Create (0.3ms)   INSERT INTO "people" ("type", "name", "email", "balance", "reports_to", "dept") VALUES('Customer', 'Bert Public', 'b@public.net', 12.45, NULL, NULL)
=> #<Customer id: 3, type: "Customer", name: "Bert Public", email: "b@public.net", balance: #<BigDecimal:967ef18,'0.1245E2',8(8)>, 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!
  Employee Create (0.3ms)   INSERT INTO "people" ("type", "name", "email", "balance", "reports_to", "dept") VALUES('Employee', 'Barney Rub', 'barney@here.com', NULL, 2, 23)
=> true
 
>> manager = Person.find_by_name("Wilma Flint")
  Person Load (0.6ms)   SELECT * 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")
  Person Load (0.5ms)   SELECT * FROM "people" WHERE ("people"."name" = 'Bert Public') LIMIT 1
=> #<Customer id: 3, type: "Customer", name: "Bert Public", email: "b@public.net", balance: #<BigDecimal:97c4378,'0.1245E2',8(8)>, 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")
  Person Load (0.4ms)   SELECT * 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>
>> p b.boss
  Manager Load (0.6ms)   SELECT * FROM "people" WHERE ("people"."id" = 2) AND ( ("people"."type" = 'Manager' ) ) 
#<Manager id: 2, type: "Manager", name: "Wilma Flint", email: "wilma@here.com", balance: nil, reports_to: nil, dept: 23>
=> #<Manager id: 2, type: "Manager", name: "Wilma Flint", email: "wilma@here.com", balance: nil, reports_to: nil, dept: 23>
 
irb e1/ar/polymorphic.rb 1
Switch to inspect mode.
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/git/awdwr/work-192-236/migration/app/controllers/", "/home/rubys/git/awdwr/work-192-236/migration/app", "/home/rubys/git/awdwr/work-192-236/migration/app/models", "/home/rubys/git/awdwr/work-192-236/migration/app/controllers", "/home/rubys/git/awdwr/work-192-236/migration/app/helpers", "/home/rubys/git/awdwr/work-192-236/migration/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib/../builtin/rails_info/", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activesupport/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionpack/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activerecord/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionmailer/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activeresource/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/i18n-0.4.0.beta1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mail-2.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mime-types-1.16/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-mount-0.6.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-format-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-test-0.5.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/tzinfo-0.3.20/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-scp-1.0.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/highline-1.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-gateway-1.0.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/ext", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/arel-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/builder-2.1.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/htmlentities-4.2.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-2.0.22/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/polyglot-0.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/will_paginate-3.0.pre/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-sftp-2.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/activesupport-3.0.0.beta3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/lib", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1/i686-linux", "/home/rubys/git/awdwr/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> []
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> []
>> require "active_record"
=> []
 
>> 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.1350s
-- create_table(:articles, {:force=>true})
   -> 0.0041s
-- create_table(:sounds, {:force=>true})
   -> 0.0037s
-- create_table(:images, {:force=>true})
   -> 0.0037s
=> 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">
=> #<Article id: 1, content: "This is my new article">
 
sqlite3> select * from articles
     id = 1
content = This is my new article
sqlite3> select * from catalog_entries
           id = 1
         name = Article One
  acquired_at = 2010-05-24 10:33:33
  resource_id = 1
resource_type = Article
sqlite3> delete from catalog_entries
irb e1/ar/polymorphic.rb 2
Switch to inspect mode.
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/git/awdwr/work-192-236/migration/app/controllers/", "/home/rubys/git/awdwr/work-192-236/migration/app", "/home/rubys/git/awdwr/work-192-236/migration/app/models", "/home/rubys/git/awdwr/work-192-236/migration/app/controllers", "/home/rubys/git/awdwr/work-192-236/migration/app/helpers", "/home/rubys/git/awdwr/work-192-236/migration/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib/../builtin/rails_info/", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activesupport/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionpack/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activerecord/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionmailer/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activeresource/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/i18n-0.4.0.beta1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mail-2.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mime-types-1.16/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-mount-0.6.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-format-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-test-0.5.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/tzinfo-0.3.20/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-scp-1.0.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/highline-1.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-gateway-1.0.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/ext", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/arel-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/builder-2.1.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/htmlentities-4.2.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-2.0.22/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/polyglot-0.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/will_paginate-3.0.pre/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-sftp-2.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/activesupport-3.0.0.beta3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/lib", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1/i686-linux", "/home/rubys/git/awdwr/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> []
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> []
>> require "active_record"
=> []
 
>> 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: "2010-05-24 10:33:38", resource_id: 2, resource_type: "Article">, #<CatalogEntry id: 3, name: "Image One", acquired_at: "2010-05-24 10:33:38", resource_id: 1, resource_type: "Image">, #<CatalogEntry id: 4, name: "Sound One", acquired_at: "2010-05-24 10:33:38", 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 = 2010-05-24 10:33:38
  resource_id = 2
resource_type = Article
 
           id = 3
         name = Image One
  acquired_at = 2010-05-24 10:33:38
  resource_id = 1
resource_type = Image
 
           id = 4
         name = Sound One
  acquired_at = 2010-05-24 10:33:38
  resource_id = 1
resource_type = Sound
irb e1/ar/self_association.rb
Switch to inspect mode.
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/git/awdwr/work-192-236/migration/app/controllers/", "/home/rubys/git/awdwr/work-192-236/migration/app", "/home/rubys/git/awdwr/work-192-236/migration/app/models", "/home/rubys/git/awdwr/work-192-236/migration/app/controllers", "/home/rubys/git/awdwr/work-192-236/migration/app/helpers", "/home/rubys/git/awdwr/work-192-236/migration/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib/../builtin/rails_info/", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activesupport/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionpack/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activerecord/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionmailer/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activeresource/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/i18n-0.4.0.beta1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mail-2.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mime-types-1.16/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-mount-0.6.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-format-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-test-0.5.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/tzinfo-0.3.20/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-scp-1.0.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/highline-1.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-gateway-1.0.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/ext", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/arel-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/builder-2.1.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/htmlentities-4.2.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-2.0.22/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/polyglot-0.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/will_paginate-3.0.pre/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-sftp-2.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/activesupport-3.0.0.beta3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/lib", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1/i686-linux", "/home/rubys/git/awdwr/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> []
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> []
>> require "active_record"
=> []
 
>> 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.1341s
=> 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
=> [#<ActiveSupport::Callbacks::Callback:0x9627344 @kind=:validate, @method=:validate_associated_records_for_mentored_employees, @identifier=nil, @options={}>, #<ActiveSupport::Callbacks::Callback:0x9635db8 @kind=:validate, @method=:validate_associated_records_for_managed_employees, @identifier=nil, @options={}>]
 
?> 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"]
=> ["Clem", "Dawn"]
>> p adam.mentored_employees                  # => []
[]
=> []
>> p dawn.mentor.name                         # => "Clem"
"Clem"
=> "Clem"
 
 
 
 
=> nil
irb e1/ar/acts_as_list.rb
Switch to inspect mode.
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/git/awdwr/work-192-236/migration/app/controllers/", "/home/rubys/git/awdwr/work-192-236/migration/app", "/home/rubys/git/awdwr/work-192-236/migration/app/models", "/home/rubys/git/awdwr/work-192-236/migration/app/controllers", "/home/rubys/git/awdwr/work-192-236/migration/app/helpers", "/home/rubys/git/awdwr/work-192-236/migration/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib/../builtin/rails_info/", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activesupport/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionpack/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activerecord/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionmailer/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activeresource/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/i18n-0.4.0.beta1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mail-2.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mime-types-1.16/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-mount-0.6.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-format-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-test-0.5.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/tzinfo-0.3.20/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-scp-1.0.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/highline-1.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-gateway-1.0.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/ext", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/arel-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/builder-2.1.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/htmlentities-4.2.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-2.0.22/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/polyglot-0.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/will_paginate-3.0.pre/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-sftp-2.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/activesupport-3.0.0.beta3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/lib", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1/i686-linux", "/home/rubys/git/awdwr/data/code/e1/ar"]
>> require "connect"
=> true
 
>> require "logger"
=> []
>> require "rubygems"
=> []
>> require "active_record"
=> []
 
>> require "./vendor/plugins/acts_as_list/init"
=> []
 
>> #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
=> [#<ActiveSupport::Callbacks::Callback:0x8473d50 @kind=:validate, @method=:validate_associated_records_for_children, @identifier=nil, @options={}>]
 
>> class Child < ActiveRecord::Base
>>   belongs_to :parent
>>   acts_as_list  :scope => :parent
>> end
=> [#<ActiveSupport::Callbacks::Callback:0x8241834 @kind=:before_create, @method=:add_to_list_bottom, @identifier=nil, @options={}>]
 
 
?> 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
Switch to inspect mode.
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/git/awdwr/work-192-236/migration/app/controllers/", "/home/rubys/git/awdwr/work-192-236/migration/app", "/home/rubys/git/awdwr/work-192-236/migration/app/models", "/home/rubys/git/awdwr/work-192-236/migration/app/controllers", "/home/rubys/git/awdwr/work-192-236/migration/app/helpers", "/home/rubys/git/awdwr/work-192-236/migration/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib/../builtin/rails_info/", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activesupport/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionpack/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activerecord/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionmailer/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activeresource/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/i18n-0.4.0.beta1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mail-2.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mime-types-1.16/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-mount-0.6.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-format-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-test-0.5.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/tzinfo-0.3.20/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-scp-1.0.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/highline-1.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-gateway-1.0.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/ext", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/arel-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/builder-2.1.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/htmlentities-4.2.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-2.0.22/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/polyglot-0.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/will_paginate-3.0.pre/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-sftp-2.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/activesupport-3.0.0.beta3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/lib", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1/i686-linux", "/home/rubys/git/awdwr/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> []
>> require "rubygems"
=> []
>> require "active_record"
=> []
 
>> require "./vendor/plugins/acts_as_tree/lib/active_record/acts/tree.rb"
=> []
>> require "./vendor/plugins/acts_as_tree/init"
=> []
 
>> #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.1360s
=> 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
Switch to inspect mode.
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/git/awdwr/work-192-236/migration/app/controllers/", "/home/rubys/git/awdwr/work-192-236/migration/app", "/home/rubys/git/awdwr/work-192-236/migration/app/models", "/home/rubys/git/awdwr/work-192-236/migration/app/controllers", "/home/rubys/git/awdwr/work-192-236/migration/app/helpers", "/home/rubys/git/awdwr/work-192-236/migration/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib/../builtin/rails_info/", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activesupport/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionpack/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activerecord/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionmailer/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activeresource/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/i18n-0.4.0.beta1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mail-2.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mime-types-1.16/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-mount-0.6.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-format-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-test-0.5.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/tzinfo-0.3.20/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-scp-1.0.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/highline-1.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-gateway-1.0.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/ext", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/arel-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/builder-2.1.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/htmlentities-4.2.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-2.0.22/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/polyglot-0.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/will_paginate-3.0.pre/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-sftp-2.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/activesupport-3.0.0.beta3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/lib", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1/i686-linux", "/home/rubys/git/awdwr/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> []
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> []
>> require "active_record"
=> []
 
>> 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.1349s
-- create_table(:orders, {:force=>true})
   -> 0.0089s
=> 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: "2010-05-24 10:33:56">
 
 
 
>> order = Order.find(1)
=> #<Order id: 1, name: "Dave", email: "dave@xxx", address: "123 Main St", pay_type: "credit", shipped_at: "2010-05-24 10:33:56">
 
>> 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>
=> #<Invoice id: 1, order_id: 1>
 
>> 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
=> 2
 
irb e1/ar/counters.rb
Switch to inspect mode.
>> $: << File.dirname(__FILE__)
=> ["/home/rubys/git/awdwr/work-192-236/migration/app/controllers/", "/home/rubys/git/awdwr/work-192-236/migration/app", "/home/rubys/git/awdwr/work-192-236/migration/app/models", "/home/rubys/git/awdwr/work-192-236/migration/app/controllers", "/home/rubys/git/awdwr/work-192-236/migration/app/helpers", "/home/rubys/git/awdwr/work-192-236/migration/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib/../builtin/rails_info/", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/railties/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activesupport/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionpack/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activerecord/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/actionmailer/lib", "/home/rubys/git/awdwr/work-192-236/migration/vendor/rails/activeresource/lib", "/home/rubys/git/gorp/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/memcache-client-1.8.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/i18n-0.4.0.beta1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rdoc-2.5.8/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mail-2.2.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/mime-types-1.16/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-mount-0.6.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-format-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-test-0.5.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/abstract-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/tzinfo-0.3.20/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/text-hyphen-1.0.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/bundler-0.9.24/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/thor-0.13.6/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-scp-1.0.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/highline-1.5.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/rack-1.1.0/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/treetop-1.4.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/capistrano-2.5.18/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/test-unit-2.0.7/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-gateway-1.0.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/ext", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/sqlite3-ruby-1.2.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/arel-0.3.3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/builder-2.1.2/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/htmlentities-4.2.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-ssh-2.0.22/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/polyglot-0.3.1/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/will_paginate-3.0.pre/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/net-sftp-2.0.4/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/activesupport-3.0.0.beta3/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991/gems/erubis-2.6.5/lib", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/bin", "/home/rubys/.rvm/gems/ruby-1.9.2-r27991%global/gems/rake-0.8.7/lib", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/site_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby/1.9.1/i686-linux", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/vendor_ruby", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1", "/home/rubys/.rvm/rubies/ruby-1.9.2-r27991/lib/ruby/1.9.1/i686-linux", "/home/rubys/git/awdwr/data/code/e1/ar"]
>> require "connect"
=> true
>> require "logger"
=> []
 
>> #ActiveRecord::Base.logger = Logger.new(STDERR)
 
?> require "rubygems"
=> []
>> require "active_record"
=> []
 
 
>> 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.1376s
-- create_table(:line_items, {:force=>true})
   -> 0.0070s
=> nil
 
>> class Product < ActiveRecord::Base
>>   has_many :line_items
>> end
=> [#<ActiveSupport::Callbacks::Callback:0x8d80d58 @kind=:validate, @method=:validate_associated_records_for_line_items, @identifier=nil, @options={}>]
 
?> 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