#--- # Excerpted from "Agile Web Development with Rails", # published by The Pragmatic Bookshelf. # Copyrights apply to this code. It may not be used to create training material, # courses, books, articles, and the like. Contact us if you are in doubt. # We make no guarantees that this code is fit for any purpose. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information. #--- #--- # Excerpted from "Agile Web Development with Rails, 4rd Ed.", # published by The Pragmatic Bookshelf. # Copyrights apply to this code. It may not be used to create training material, # courses, books, articles, and the like. Contact us if you are in doubt. # We make no guarantees that this code is fit for any purpose. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information. #--- $: << File.dirname(__FILE__) require "connect" 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 class Product < ActiveRecord::Base has_many :line_items end class LineItem < ActiveRecord::Base belongs_to :product, :counter_cache => true end product = Product.create(:title => "Programming Ruby", :description => " ... ") line_item = LineItem.new line_item.product = product line_item.save puts "In memory size = #{product.line_items.size}" #=> 0 puts "Refreshed size = #{product.line_items(:refresh).size}" #=> 1 LineItem.delete_all Product.delete_all product = Product.create(:title => "Programming Ruby", :description => " ... ") product.line_items.create puts "In memory size = #{product.line_items.size}" #=> 1 puts "Refreshed size = #{product.line_items(:refresh).size}" #=> 1