记录更新中的Friendly_id更改slug

时间:2015-01-22 06:25:03

标签: ruby-on-rails ruby ruby-on-rails-4 friendly-id

我做错了吗? 在记录更新时,slug未更新

 class Company < ActiveRecord::Base
 extend FriendlyId

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

 def should_generate_new_friendly_id?
     true
 end

我正在使用:

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

这就是我在终端尝试的方式:

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

在创建时:它插入了slug但在记录更新时没有更新。任何想法?

2 个答案:

答案 0 :(得分:5)

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

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

并覆盖should_generate_new_friendly_id方法以满足您的需求:

 def should_generate_new_friendly_id? #will change the slug if the name changed
    name_changed?
  end

答案 1 :(得分:0)

根据以下帖子,在记录更新中,在friendly_id 5及以上版本中不会更改slug。

Rails 4 Friendly Id Slug Not Updating