fix: Add slug to articles (#5500)

This commit is contained in:
Tejaswini Chile
2022-09-28 00:57:18 +05:30
committed by GitHub
parent 8e5d8fcdaf
commit c1c57fb2cd
7 changed files with 46 additions and 14 deletions

View File

@@ -0,0 +1,21 @@
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