Chatwoot/db/migrate/20220926164441_add_slug_to_article.rb

22 lines
503 B
Ruby
Raw Normal View History

2022-09-27 19:27:18 +00:00
class AddSlugToArticle < ActiveRecord::Migration[6.1]
def up
add_column :articles, :slug, :string
update_past_articles_with_slug
add_index :articles, :slug
change_column_null(:articles, :slug, false)
end
def down
remove_column(:articles, :slug)
end
def update_past_articles_with_slug
Article.all.each_with_index do |article, index|
slug = article.title.underscore.parameterize(separator: '-')
article.update!(slug: "#{slug}-#{index}")
end
end
end