-
-
Notifications
You must be signed in to change notification settings - Fork 592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't explicitly set slug when using scoped
#983
Comments
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hey there @joe-reich! I just run into the same issue... were you able to find a workaround? |
@luctus I monkey-patched def should_generate_new_friendly_id?
# hopeless if the base for setting slug is nil
return false if public_send(friendly_id_config.base).nil?
# Slug is nil. Gotta try.
return true if public_send(friendly_id_config.slug_column).nil?
# Slug was set explicitly. Leave it.
return false if friendly_id_config.slug_column.in?(changed)
# Base for setting slug changed. Change the slug.
return true if slug_base_changed?
# Basis for scoping has changed. Reset slug.
return true if scoping_base_changed?
false
end
# Can be overwritten in the model
def slug_base_changed?
friendly_id_config.base.to_s.in?(changed)
end
private
def scoping_base_changed?
return false unless friendly_id_config.uses?(:scoped)
(changed & friendly_id_config.scope_columns).any?
end |
One of our model had a non-scoped slug for many years and creating directly with a manual slug, when user specify it, is established behavior. We have a change of model and now, that slug is scoped. This bug sadly breaks the slug overriding at creation time that our users are used to. We can of course patch this in the controller, updating right behind with the slug if it's in the parameters, but it would simply be cleaner to solve it. I will try to find time to propose a PR, but to be realistic, I do not see that happening in the near future. I am however writing those words to emphasis our interest in it being solved at some point, if a maintainer can find the time. Many thanks in advance. |
The documentation says that the class MyModel
extend FriendlyId
friendly_id :base_attr, use: %i[scoped slugged], scope: :scope_attr
...
# Override method added by FriendlyId::Scoped to prevent overwriting explicitly set slugs.
def should_generate_new_friendly_id?
send(friendly_id_config.slug_column).nil? && super
end
end |
I've also ran into this, very confusing... I'm not sure if this behaviour (force new slug when scoped column is changed) is bug or it is required for some reason? we used this approach to fix this
|
Hiya. Thanks for creating and maintaining this gem.
I ran into an issue: explicitly setting the slug doesn't seem to work when the model uses
scoped
.Here's some pseudo-rails to give the gist.
Without scoped, it works fine:
With scoped, it runs into trouble:
Looks like the issue is here:
friendly_id/lib/friendly_id/scoped.rb
Lines 138 to 140 in 81442a6
Within the gem codebase, it looks like this issue only arises in
Scoped
. But it would also arise for users who followed the suggestion to redefineshould_generate_new_friendly_id?
in their models.If the maintainers agree with what I'm suggesting is the expected behavior, I might be able to find time to put a PR together.
The text was updated successfully, but these errors were encountered: