slugged icon indicating copy to clipboard operation
slugged copied to clipboard

have slugged use a scope to verify uniqueness?

Open JeanMertz opened this issue 13 years ago • 2 comments

Hey,

I was wondering if it is possible to have slugged check a specific scope before deciding if a slug is unique or not (and in the process adding --1, --2, etc)? I have a title field and account_id field and only want slugged to do this when both the title and account_id are equal to a database row.

Is this possible? Thanks.

Edit:

If this feature isn't already built in, I guess this would also mean I'd have to rewrite the find_using_slug method(s) to take this into account, right? I really hope this is already a built in feature and/or easy to implement, as it's quite crucial to my app.

Thanks in advance!

JeanMertz avatar Mar 18 '11 16:03 JeanMertz

Well, after trying to figure this out, I am kind of back to square one for now.

Some things I tried:

  1. adding :scope => :account_id to the is_sluggable options didn't do the trick.
  2. even if creating the slug didn't work yet, I tried getting a record using current_account.pages (which limit the searched pages scoped by account_id), but somehow current_account.pages.find_using_slug(slug) still searched all other records.
  3. Since friendly_id already has this feature I tried to switch to friendy_id but it has so many options and features that I don't want that I stopped half way through and reverted to the previous commit with slugged.
  4. ... going to try other things now, hope I can get a response soon or even better I figure it out myself.

Thanks again.

JeanMertz avatar Mar 19 '11 10:03 JeanMertz

I kind of hacked around the find_using_slug issue by overriding the method to use find_by_cached_slug(). This obviously is bad as it no longer allows history or id's to be passed in, but at least it works for now (until you/I come up with a solution).

This won't fix the issue of creating a new page on account 2 that already exists on account 1, resulting in "my-title--2" as the slug instead of "my-title", but it's at least something I can work with during testing.

JeanMertz avatar Mar 19 '11 12:03 JeanMertz