associations.rb 3.3 KB

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