我張貼我自己解決這個問題,以防萬一有人有同樣的問題。我應該重申,在friendly_id
gem(當時是最新版本)版本4.1.0.beta.1
上發現了此問題,因此此問題可能不再發生。
爲了解決這個問題,我基本上配置了slug_generator_class
來使用我自己的類,所以我可以用猴子補丁的罪魁禍首的方法。
在我的模型:
friendly_id do |config|
config.slug_generator_class = SubclassScopableSlugGenerator
end
在初始化,我推翻了FriendlyId::SlugGenerator.conflicts
方法,所以我可以訪問sluggable_class
VAR:
# Lets a non-STI subclass of a FriendlyId parent (i.e. a subclass with its
# own dedicated table) have independent slug uniqueness.
class SubclassScopableSlugGenerator < FriendlyId::SlugGenerator
private
def conflicts
# this is the only line we're actually changing
sluggable_class = friendly_id_config.model_class
pkey = sluggable_class.primary_key
value = sluggable.send pkey
base = "#{column} = ? OR #{column} LIKE ?"
# Awful hack for SQLite3, which does not pick up '\' as the escape character without this.
base << "ESCAPE '\\'" if sluggable.connection.adapter_name =~ /sqlite/i
scope = sluggable_class.unscoped.where(base, normalized, wildcard)
scope = scope.where("#{pkey} <> ?", value) unless sluggable.new_record?
length_command = "LENGTH"
length_command = "LEN" if sluggable.connection.adapter_name =~ /sqlserver/i
scope = scope.order("#{length_command}(#{column}) DESC, #{column} DESC")
end
end