-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Nullify duplicate slugs under the same parent after reodering pages. #2092
Conversation
Updated with |
This could also be done using: def nullify_duplicate_slugs_under_the_same_parent!
table = Page.translation_class.arel_table
Page.includes(:translations).group(:parent_id, table[:slug]).having(table[:slug].count.gt(1)).count(&:slug).each do |(parent_id, slug), count|
Page.by_slug(slug).where(:parent_id => parent_id).drop(1).each do |page|
page.slug = nil
page.save
end
end
end |
I'm afraid that we're also going to have problems with slugs in locales other than the current locale too >_> |
It could be done with an arel table and I assume the performance would be better because it's doing more of the filtering in the database. However, if the performance gain isn't noticeable I think the way it is is easier to read. With the arel table do you need to pass |
OK I've dealt with the locale issues and force pushed. Good to go 👍 |
…ter reodering pages. Fixes #2070
Nullify duplicate slugs under the same parent after reodering pages.
Thanks @gwagener for |
I smell a blog post @ugisozols |
protected | ||
def nullify_duplicate_slugs_under_the_same_parent! | ||
t_slug = translation_class.arel_table[:slug] | ||
joins(:translations).group(:locale, :parent_id, t_slug).having(t_slug.count.gt(1)).count. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ERROR: column "refinery_pages.lft" must appear in the GROUP BY clause or be used in an aggregate functio
Correct could be:
def nullify_duplicate_slugs_under_the_same_parent!
t_slug = translation_class.arel_table[:slug]
joins(:translations).group(:locale, :parent_id, t_slug, :lft).having(t_slug.count.gt(1)).count.
each do |(locale, parent_id, slug), count|
by_slug(slug, :locale => locale).where(:parent_id => parent_id).drop(1).each do |page|
page.slug = nil # kill the duplicate slug
page.save # regenerate the slug
end
end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@viglesias can you open a pull request with a failing test case if you're having issues please?
Fixes #2070