Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Friendly_id change slug on Record Update

Am i doing something wrong ? on Record update, slug is not being updated.

 class Company < ActiveRecord::Base
 extend FriendlyId

 friendly_id :name, use: [:slugged, :finders]

 def should_generate_new_friendly_id?
     true
 end

I'm using:

 friendly_id (5.0.4)
 Rails 4.1.7
 ruby 2.1.3p242 (2014-09-19 revision 47630)

This is how I'm trying in terminal:

  c = Company.last
  c.slug = nil 
  c.name = "testing abb"
  c.save
  c.reload
  c.slug // nil 
  c.name // testing abb

at the time of create: it inserts the slug but doesnt update on record update. any idea ?

like image 984
kashif Avatar asked Mar 17 '23 07:03

kashif


1 Answers

Add :history to friendly_id :name, use: %i(slugged finders):

friendly_id :name, use: %i(slugged history finders)

and override should_generate_new_friendly_id method to meet your needs:

 def should_generate_new_friendly_id? #will change the slug if the name changed
    name_changed?
  end
like image 51
Andrey Deineko Avatar answered Mar 29 '23 09:03

Andrey Deineko