class TbCommerce::Category < ActiveRecord::Base self.table_name = 'tb_commerce_categories' scope :ordered, ->{ order('title asc') } validates :title, :presence => true, :length => {:maximum => 255}, :uniqueness => true before_save :generate_slug_from_title has_many :products, :inverse_of => :category, :foreign_key => :tb_commerce_category_id, :dependent => :nullify has_many :images, :as => :imageable private def generate_slug_from_title if slug.blank? || title_changed? self.slug = title.parameterize end end end