order.rb 846 B

12345678910111213141516171819202122
  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. attr_accessible :address, :email, :name, :pay_type
  12. has_many :line_items, dependent: :destroy
  13. # ...
  14. validates :name, :address, :email, presence: true
  15. validates :pay_type, inclusion: PAYMENT_TYPES
  16. def add_line_items_from_cart(cart)
  17. cart.line_items.each do |item|
  18. item.cart_id = nil
  19. line_items << item
  20. end
  21. end
  22. end