Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

23 řádky
692 B

  1. class Product < ApplicationRecord
  2. has_many :line_items
  3. has_many :orders, through: :line_items
  4. before_destroy :ensure_not_referenced_by_any_line_item
  5. validates :title, :description, :image_url, :price, presence: true
  6. validates :price, numericality: {greater_than_or_equal_to: 0.01}
  7. validates :title, uniqueness: true, length: { minimum: 5, maximum: 60 }
  8. validates :image_url, allow_blank: true, format: {
  9. with: %r{\.(gif|jpg|png)\Z}i,
  10. message: 'must be URL for GIF, JPG or PNG image.'
  11. }
  12. private
  13. def ensure_not_referenced_by_any_line_item
  14. unless line_items.empty?
  15. errors.add(:base, 'Line Items present')
  16. throw :abort
  17. end
  18. end
  19. end