FriendlyId没有加入顺序和投掷的ActiveRecord :: RecordNotUnique [英] FriendlyId not adding sequence and throwing ActiveRecord::RecordNotUnique
问题描述
上下文是pretty的简单,我有一个课程模型,从FriendlyId扩展如下:
The context is pretty simple, I have a Course model that extends from FriendlyId as follow:
extend FriendlyId
friendly_id :friendly_name, use: [:slugged, :history]
def friendly_name
slugs = [self.type_name, self.name]
slugs << self.city.name if self.city
slugs << self.structure.name if self.structure
return slugs
end
如果我创建课程具有相同的类型,名称,城市和结构,我得到了以下错误:
And if I create a course with same type, name, city and structure I get the following error:
!! #<ActiveRecord::RecordNotUnique: PG::Error: ERROR: duplicate key value
violates unique constraint "index_courses_on_slug"
DETAIL: Key (slug)=(cours-sevillanas-copie-paris-12-la-trianera) already exists.
我不明白为什么FriendlyId没有在塞的末尾添加一个序列号... 任何建议表示欢迎。
I don't understand why FriendlyId doesn't add a sequence number at the end of the slug... Any suggestion is welcomed.
我试图在friendly_name方法返回数组的字符串代替,但错误依然存在。
I have tried to return a string instead of an array in the friendly_name method but the error persists.
修改
删除:历史修复这个问题。
Removing :history fixes the problem.
I'have也试过其他部门FriendlyId的(4.0-稳定,4.1.x版),但它并没有解决问题。
I'have also tried other branches (4.0-stable, 4.1.x) of FriendlyId but it doesn't fixes the problem.
推荐答案
我有同样的问题的:历史的特点:这是因为FriendlyId将使用一个单独的表来存储蛞蝓,并不会检查现有塞柱
I had the same problem with the :history features: it's because FriendlyId will use a separate table to store slugs, and will not check the existing slug column.
您可以创建一个迁移和重新保存整个表生成缺少的蛞蝓在新的蛞蝓表。
You can create a migration and re-save the whole table to generate missing slugs in the new slugs table.
例如:
def up
MyModel.all.map(&:save)
end
这篇关于FriendlyId没有加入顺序和投掷的ActiveRecord :: RecordNotUnique的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!