order.rb 793 B

123456789101112131415161718192021
  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. class Order < ActiveRecord::Base
  10. PAYMENT_TYPES = [ "Check", "Credit card", "Purchase order" ]
  11. has_many :line_items, dependent: :destroy
  12. # ...
  13. validates :name, :address, :email, presence: true
  14. validates :pay_type, inclusion: PAYMENT_TYPES
  15. def add_line_items_from_cart(cart)
  16. cart.line_items.each do |item|
  17. item.cart_id = nil
  18. line_items << item
  19. end
  20. end
  21. end