associations.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. $: << File.dirname(__FILE__)
  10. require "connect"
  11. require "logger"
  12. #ActiveRecord::Base.logger = Logger.new(STDERR)
  13. require "rubygems"
  14. require "active_record"
  15. ActiveRecord::Schema.define do
  16. create_table :products, :force => true do |t|
  17. t.string :title
  18. t.text :description
  19. t.string :image_url
  20. t.decimal :price, :precision => 10, :scale => 2
  21. t.datetime :available_at
  22. end
  23. create_table :line_items, :force => true do |t|
  24. t.integer :product_id
  25. t.integer :order_id
  26. t.integer :quantity
  27. t.integer :unit_price, :precision => 8, :scale => 2
  28. end
  29. end
  30. class Product < ActiveRecord::Base
  31. has_many :line_items
  32. end
  33. class LineItem < ActiveRecord::Base
  34. belongs_to :product
  35. end
  36. LineItem.delete_all
  37. p = Product.create(:title => "Programming Ruby", :available_at => Time.now)
  38. l = LineItem.new
  39. l.id = 2
  40. l.product = p
  41. l.save!
  42. l = LineItem.new
  43. l.product = p
  44. l.save!
  45. puts "\n\nSimple Belongs to"
  46. item = LineItem.find(2)
  47. # item.product is the associated Product object
  48. puts "Current product is #{item.product.id}"
  49. puts item.product.title
  50. item.product = Product.new(:title => "Rails for Java Developers",
  51. :description => "...",
  52. :image_url => "http://....jpg",
  53. :price => 34.95,
  54. :available_at => Time.now)
  55. item.save!
  56. puts "New product is #{item.product.id}"
  57. puts item.product.title
  58. puts "\n\nCreate belongs to"
  59. item = LineItem.find(2)
  60. # item.product is the associated Product object
  61. puts "Current product is #{item.product.id}"
  62. puts item.product.title
  63. item.create_product(:title => "Rails Recipes",
  64. :description => "...",
  65. :image_url => "http://....jpg",
  66. :price => 32.95,
  67. :available_at => Time.now)
  68. puts "New product is #{item.product.id}"
  69. puts item.product.title
  70. puts "\n\nproduct belongs to"
  71. item = LineItem.find(2)
  72. # item.product is the associated Product object
  73. puts "Current product is #{item.product.id}"
  74. puts item.product.title
  75. item = LineItem.new()
  76. item.create_product(:title => "Advanced Rails",
  77. :description => "...",
  78. :image_url => "http://....jpg",
  79. :price => 34.95,
  80. :available_at => Time.now)
  81. puts "New product is #{item.product.id}"
  82. puts item.product.title
  83. item.save!
  84. p item.product(true)
  85. puts "============="
  86. prod = Product.find(item.product_id)
  87. p prod.line_items.size