I'm using the following:
gem 'friendly_id', github: 'FriendlyId/friendly_id', branch: 'master'
I am creating an Article section on my Rails 4 website. The problem I am having is that when I change a existing article's name the slug is not updated.
This is what I have so far:
extend FriendlyId
friendly_id :name, use: :slugged
add_column :articles, :slug, :string
add_index :articles, :slug, unique: true
In FriendlyId 4 (Rails 3 compatible) there was a method
and you could define it on your model to control when slug is regenerated. Try
to regenerate slug when name changes.
EDIT
FriendlyId version 5 (Rails 4 compatible) doesn't regenerate slugs on save anymore. To restore this functionality you can either set slug column to
nil
before saving or use the solution provided above.EDIT 2
You need to override the slug setter for your saves to work for Rails <5 & FriendlyId > 5 as referenced in this issue.
Add this to the model file
I have this issues and just want to point out what I've noticed.
if you only do as in docs
and then run
Post.find_each(&:save)
- slug is gonna get updated...However in my case, I also have these in my model
with the code above it won't do anything when you run
Post.find_each(&:save)
I assume since your title doesn't change. (first method handles russian language)so when working with the first model all worked great, but then when I copied ready code to next model I wanted to slugify, I run into some issues. Hope it helps someone.