123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #---
- # 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
|