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

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_09_20_014549) do
ActiveRecord::Schema.define(version: 2022_09_26_164441) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
@@ -131,8 +131,10 @@ ActiveRecord::Schema.define(version: 2022_09_20_014549) do
t.bigint "author_id"
t.bigint "associated_article_id"
t.jsonb "meta", default: {}
t.string "slug", null: false
t.index ["associated_article_id"], name: "index_articles_on_associated_article_id"
t.index ["author_id"], name: "index_articles_on_author_id"
t.index ["slug"], name: "index_articles_on_slug"
end
create_table "attachments", id: :serial, force: :cascade do |t|