0

I'm migrating a very old app from friendly_id 3.2 to 5.1.

I have a user model which currently has a cached_slug field. I created a new field called just slug. Initially, I thought I could just copy the data over from cached_slug to slug, but I noticed that there's also a whole other table called slugs which looks like this:

  create_table "slugs", force: :cascade do |t|
    t.string   "name",           limit: 255
    t.integer  "sluggable_id"
    t.integer  "sequence",                   default: 1, null: false
    t.string   "sluggable_type", limit: 40
    t.string   "scope",          limit: 255
    t.datetime "created_at"
  end

Following the directions in the FriendlyID README for the Rails Quickstart, I ran rails generate friendly_id which created this table:

create_table "friendly_id_slugs", force: :cascade do |t|
    t.string   "slug",                      null: false
    t.integer  "sluggable_id",              null: false
    t.string   "sluggable_type", limit: 50
    t.string   "scope"
    t.datetime "created_at"
  end

So now I'm all confused about what I need to do to complete migration. I tried creating a new user with the console and the friendly_id_slugs table is still empty, so I'm not sure when or what it's used for.

I have two questions: 1) What is this other table used for 2) What do I need to do to migrate my old slugs to the new table/field so it will continue to work moving forward?

Thanks!

Vika Marquez
  • 353
  • 1
  • 3
  • 12
Jeff
  • 1,051
  • 1
  • 13
  • 26

1 Answers1

1

If you don't mind losing the FriendlyId's History records (i.e: the old slugs),

  1. drop the old slug table
  2. in the rails console, run <ModelName>.find_each(&:save) for all friendly_id models.

Step 2 should regenerate new slugs.

Warning: Test before doing this on production servers!

Update: You can also perform step 2 in a migration file

class RegenerateSlugs < ActiveRecord::Migration
    def change
        # <ModelName>.find_each(&:save)
        User.find_each(&:save)
        Article.find_each(&:save)
    end
end
tangrufus
  • 357
  • 2
  • 13
  • I'd like to keep old slugs for SEO reasons. It read through some of the source this weekend, and it looks like I don't need all extra tables unless I want to use :history. So I could just copy from the old cached_slugs into the new .slug field, I may use some historical context, but that's a very small % of my use cases, so should be good enough. Thanks! – Jeff May 20 '15 at 16:44