Question

I'm current using acts_as_paranoid and friendly_id (5.0.1) on a model and when I destroy a model and try to create a new one that will generate the same slug I get:

ERROR:  duplicate key value violates unique constraint "index_papers_on_slug"

I need to somehow get the code that checks if a slug already exists check within the scope of all of the objects not just the non-deleted ones.

How can I get friendly_id to use with_deleted when checking if a slug already exists. I should note that I am also using slug history which may be complicating things further.

Upon digging deeper I realized that since I am using history the slug is being fully deleted while the object is just being soft 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 okay since it looks like the friendly_id code itself is already using unscoped when trying to find a valid slug.

Was it helpful?

Solution 2

Adding the following to the model allowed me to overrride the dependent destroy on the slugs

def has_many_dependent_for_slugs; end

The solution comes from a comment on this github issue.

OTHER TIPS

Friendly_id has a module called scoped which allows you to generate unique slugs within a scope. So, probably

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

will resolve the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top