class ProductCategoryLang < ActiveRecord::Base
  belongs_to :product_category
  
  validates :name, :presence => true
  
  validates :slug, :presence => true#, :uniqueness => {:scope => :lang}
  attr_accessor :parent_id
  before_validation do
    self.slug = self.name.to_slug
    if self.id?
      parent_id_test = self.product_category.parent_id 
      
    else
      parent_id_test = parent_id
    end

    search_test = ProductCategoryLang.joins(:product_category).where("lang = ?", self.lang).where("product_categories.parent_id "+(parent_id_test ? "=" : "IS")+" ? and slug = ? ", parent_id_test, self.slug)
    search_test = search_test.where("product_categories.id != ?", self.id) if self.id
    if search_test.first
      #errors.add(:slug, 'est déjà utilisé') 
      
    end
    
    
  end
end
