友好ID 4使用范围模块

我无法解决这个问题。 使用文档

我有两个型号

class Region  :slugged end class Page  :scoped, :scope => :region end 

根据文档,这应该工作。 但是当我创建一个页面时,它不会通过作用域创建一个slug,这意味着当我创建另一个具有相同名称的页面时,我会得到重复的索引错误。

如果您当前正在使用以下内容对slug字段中的页面建立索引:

 add_index :page, :slug, :unique => true 

然后你可能想把它换成slug和region的索引:

 remove_index :page, :slug add_index :page, [:slug, :region_id], :unique => true 

该文档描述了如何开始使用:history:scoped在FriendlyId 5中一起使用,也许这可以为您提供有关如何在您的案例中解决它的一些想法: http : //rubydoc.info/github/norman/friendly_id/master/file /README.md#Upgrading_from_FriendlyId_4_0


或者,您可以删除唯一性约束,因为我现在看到您已回复我的原始评论。 🙂

 remove_index :page, :slug add_index :page, :slug # no :unique here