ruby Friendly_id在记录更新时更改slug

qyswt5oh  于 2023-05-17  发布在  Ruby
关注(0)|答案(2)|浏览(128)

我做错什么了吗?记录更新时,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

创建时:它插入段塞,但在记录更新时不更新。知道吗?

gcxthw6b

gcxthw6b1#

: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
pu82cl6c

pu82cl6c2#

根据下面的帖子,在记录更新时,friendly_id 5和更高版本中的slug没有改变。
Rails 4 Friendly Id Slug Not Updating

相关问题