find_examples.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. require "pp"
  24. @params = {}
  25. def params
  26. @params
  27. end
  28. class Order < ActiveRecord::Base
  29. named_scope :check, :conditions => {:pay_type => 'check'}
  30. named_scope :cc, :conditions => {:pay_type => 'cc'}
  31. named_scope :po, :conditions => {:pay_type => 'po'}
  32. end
  33. class Order < ActiveRecord::Base
  34. named_scope :recent, :conditions => ['created_at > ?', 1.week.ago]
  35. named_scope :since, lambda { |range|
  36. { :conditions => ['created_at > ?', range] }
  37. }
  38. end
  39. class LineItem < ActiveRecord::Base
  40. end
  41. class Product < ActiveRecord::Base
  42. end
  43. # return an arbitrary order
  44. order = Order.find(:first)
  45. # return an order for Dave
  46. order = Order.find(:first, :conditions => "name = 'Dave Thomas'")
  47. # return the latest order for Dave
  48. order = Order.find(:first,
  49. :conditions => "name = 'Dave Thomas'",
  50. :order => "id DESC")
  51. orders = LineItem.find_by_sql("select line_items.* from line_items, orders " +
  52. " where order_id = orders.id " +
  53. " and orders.name = 'Dave Thomas' ")
  54. orders = Order.find_by_sql("select name, pay_type from orders")
  55. first = orders[0]
  56. p first.attributes
  57. p first.attribute_names
  58. p first.attribute_present?("address")
  59. p Order.all
  60. p Order.check(:order => "created_on desc").first
  61. p Order.po.recent.count
  62. p Order.check.find_by_name('Dave Thomas')
  63. p Order.po.recent(:order => :created_at)
  64. p Order.po.since(1.week.ago)
  65. LineItem.delete_all
  66. Product.delete_all
  67. p = Product.create(:title => "Programming Ruby", :price => 49.95)
  68. LineItem.create(:quantity => 2, :product_id => p.id, :order_id => first)
  69. items = LineItem.find_by_sql("select *, " +
  70. " products.price as unit_price, " +
  71. " quantity*products.price as total_price, " +
  72. " products.title as title " +
  73. " from line_items, products " +
  74. " where line_items.product_id = products.id ")
  75. li = items[0]
  76. puts "#{li.title}: #{li.quantity}x#{li.unit_price} => #{li.total_price}"
  77. c1 = Order.count
  78. c2 = Order.count(:conditions => ["name = ?", "Dave Thomas"])
  79. c3 = LineItem.count_by_sql("select count(*) " +
  80. " from line_items, orders " +
  81. " where line_items.order_id = orders.id " +
  82. " and orders.name = 'Dave Thomas' ")
  83. puts "Dave has #{c3} line items in #{c2} orders (#{c1} orders in all)"
  84. order = Order.find_by_name("Dave Thomas")
  85. orders = Order.find_all_by_name("Dave Thomas")
  86. orders = Order.find_all_by_email(params['email'])
  87. o = LineItem.find(:all,
  88. :conditions => "pr.title = 'Programming Ruby'",
  89. :joins => "inner join products as pr on line_items.product_id = pr.id")
  90. p o.size
  91. LineItem.delete_all
  92. res = Order.update_all("pay_type = 'wibble'")
  93. p res
  94. res = Order.delete_all(["pay_type = ?", "wibble"])
  95. p res