slugs

ruby
class AddSlugToArticles < ActiveRecord::Migration[7.1]
  def change
    add_column :articles, :slug, :string, null: false, default: ""
    add_index :articles, :slug, unique: true

    # Backfill existing rows before the unique index is relied upon in code.

Generate Unique URL Slugs in Rails with before_validation and a friendly Controller Lookup

rails activerecord slugs
by codesnips 3 tabs
python
from django.db import models
from django.utils.text import slugify


class Article(models.Model):
    title = models.CharField(max_length=200)

Auto-Generate Unique Slugs in Django with a pre_save Signal and Model Method

django signals slugs
by codesnips 3 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Generating URL Slugs from Titles with a Laravel Model Observer

laravel eloquent observers
by codesnips 4 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Slugify Post Titles on Save With a Laravel Model Observer and Guaranteed Uniqueness

laravel eloquent observer
by codesnips 4 tabs
ruby
class AddSlugToPosts < ActiveRecord::Migration[7.1]
  def change
    add_column :posts, :slug, :string, null: false
    add_index :posts, :slug, unique: true
  end
end

Database-Backed Unique Slugs with Retry

rails postgres slugs
by codesnips 4 tabs
ruby
class AddSlugToArticles < ActiveRecord::Migration[7.1]
  def change
    add_column :articles, :slug, :string
    add_index :articles, :slug, unique: true

    reversible do |dir|

Auto-Generating URL Slugs in Rails with a before_validation Callback and Friendly Finder

rails activerecord slugs
by codesnips 4 tabs