Friendly_id and act_as_paranoid creating repeating slugs

I use act_as_paranoid and friendly_id (5.0.1) in the model, and when I destroy the model and try to create a new one that will generate the same pool, I get:

ERROR:  duplicate key value violates unique constraint "index_papers_on_slug"

I need to somehow get a code that checks if an already installed slug exists within all objects, not just not deleted ones.

How can I get friendly_id to use with_deletedwhen checking if slug exists. I should note that I also use traffic history, which can complicate the situation.

When I went deeper, I realized that since I am using history, slug is completely deleted when the object is just gently deleted:

DELETE FROM "friendly_id_slugs" WHERE "friendly_id_slugs"."id" = $1  [["id", 9423]]

So, I just need to figure out how to prevent this, and I should be fine, as it looks like the friendly_id code itself is already using it unscopedwhen trying to find a valid pool.

+3
source share
2 answers

Adding the following to the model allowed me to redefine dependent kill on slugs

def has_many_dependent_for_slugs; end

The solution comes from a comment on this github issue .

+1
source

Friendly_id has a module called scopedthat allows you to create unique bullets within an area. So maybe

class Paper < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, :use => :scoped, :scope => :unscoped
end

will solve the problem.

+2
source

All Articles