def do_before_validation
      prim_slug, i = slug, 0
      prim_slug, i = $1, $2.to_i if slug =~ /^(.*)_([0-9]+)$/

      return unless prim_slug.present?

      attempts = 0
      conditions = new_record? ? ['slug = ?',slug] : ['slug = ? AND id != ?',slug,id]
      conditions[1] = "#{prim_slug}_#{i+=1}" while Page.where(conditions).first && (attempts += 1) < 20
      write_attribute(:slug,conditions[1])
    end
def do_before_validation
      prim_slug, i = slug, 0
      prim_slug, i = $1, $2.to_i if slug =~ /^(.*)_([0-9]+)$/

      return unless prim_slug.present?

      attempts = 0
      conditions = new_record? ? ['slug = ?',slug] : ['slug = ? AND id != ?',slug,id]
      conditions[1] = "#{prim_slug}_#{i+=1}" while Page.where(conditions).first && (attempts += 1) < 20
      write_attribute(:slug,conditions[1])
    end
auto increment slug until it is unique