Skip to content

Scope is not applied for new records when history module is enabled #28

Description

@sramos

For globalized slugs, when both modules are enabled, scope and history, scope is not applied for new records, only for updated ones.

You can reproduce it creating two records with the same values excepting scoped data. Slug should be the same for both, but it doesn't. To let the same value, you have to modify translated slug with the same value of first record.

I think that the problem could be in the scope_for_slug_generator method of the history module version provided for this gem. Only when record exists the scope is used to get the slug table relationship.

Changing the order of friendly_id_config.uses?(:scoped) condition all works fine and slug is generated correctly:

@@ -109,12 +109,11 @@ method.
     # used slug.
     def scope_for_slug_generator
       relation = super
-      return relation if new_record?
-      relation = relation.merge(Slug.where('sluggable_id <> ?', id))
       if friendly_id_config.uses?(:scoped)
         relation = relation.where(Slug.arel_table[:scope].eq(serialized_scope))
       end
-      relation
+      relation = relation.merge(Slug.where('sluggable_id <> ?', id)) unless new_record?
+      return relation
     end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions