new_examples.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "rubygems"
  12. require "active_record"
  13. class Order < ActiveRecord::Base
  14. end
  15. an_order = Order.new
  16. an_order.name = "Dave Thomas"
  17. an_order.email = "dave@example.com"
  18. an_order.address = "123 Main St"
  19. an_order.pay_type = "check"
  20. an_order.save
  21. Order.new do |o|
  22. o.name = "Dave Thomas"
  23. # . . .
  24. o.save
  25. end
  26. an_order = Order.new(
  27. name: "Dave Thomas",
  28. email: "dave@example.com",
  29. address: "123 Main St",
  30. pay_type: "check")
  31. an_order.save
  32. an_order = Order.new
  33. an_order.name = "Dave Thomas"
  34. # ...
  35. an_order.save
  36. puts "The ID of this order is #{an_order.id}"
  37. an_order = Order.create(
  38. name: "Dave Thomas",
  39. email: "dave@example.com",
  40. address: "123 Main St",
  41. pay_type: "check")
  42. orders = Order.create(
  43. [ { name: "Dave Thomas",
  44. email: "dave@example.com",
  45. address: "123 Main St",
  46. pay_type: "check"
  47. },
  48. { name: "Andy Hunt",
  49. email: "andy@example.com",
  50. address: "456 Gentle Drive",
  51. pay_type: "po"
  52. } ] )