Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.91 KB | None | 0 0
  1. class Product < ApplicationRecord
  2.   has_attached_file :image, styles: { medium: '100x100>', thumb: '50x50>' }
  3.   validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
  4.   validates :price, numericality: { greater_than: 0, allow_nil: true }
  5.   validates :name, presence: true
  6.  
  7.   has_many :line_items
  8.   before_destroy :ensure_not_referenced_by_any_line_item
  9.  
  10.   belongs_to :category
  11.  
  12.   def self.search(search)
  13.     where("cast(name as text) LIKE ? OR cast(price as text) LIKE ?", "%#{search}%", "%#{search}%")
  14.   end
  15.  
  16.   private
  17.   #тестирую вот этот метод, когда он приватный ошибка, когда делаю публичным всё норм.
  18.   def ensure_not_referenced_by_any_line_item
  19.     if line_items.empty?
  20.       return true
  21.     else
  22.       errors.add(:base, 'существуют товарные позиции')
  23.       return false
  24.     end
  25.   end
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement