fix: Add slug to articles (#5500)
This commit is contained in:
21
db/migrate/20220926164441_add_slug_to_article.rb
Normal file
21
db/migrate/20220926164441_add_slug_to_article.rb
Normal 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
|
||||
@@ -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|
|
||||
|
||||
Reference in New Issue
Block a user