randomInput
randomInput

Reputation: 19

Friendly_id Unique contraint with scope and history

'rails', '~> 5.1.2'
'friendly_id', '~> 5.1.0'

I'm trying to add 'scoped' to my existing slugs.

class Model < ApplicationRecord   
  extend FriendlyId   
  belongs_to :board   
  friendly_id :slug_candidates, use: [:slugged, :scoped, :history], :scope => :board

But I'm coming up with this error when a duplicate is run that should have a different scope.

ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_models_on_slug" DETAIL: Key (slug)=(xxx-xxxx-xxx) already exists.

Looking through the logs, I can see the scope is generating and lists the scope model (via board_id, xx), which I assume is as it should, but it doesn't seem to be searching the scope when saving.

If I create a duplicate under that same scope, it adds the duplicate code ("42a8d035-ba2f-4a51-b85e-0872c4e207e2").

I hope this makes sense - Any help would be appreciated.

Thanks, Andy

Upvotes: 0

Views: 873

Answers (1)

randomInput
randomInput

Reputation: 19

So it turns out I should have thought about this post a bit more.

“Key (slug)=() already exists” in friendly_id

Adding the scope on existing models meant I didn't think about but had to remove the unique=true on the existing model.

Anyway - love friendly_id great product!

Upvotes: 0

Related Questions