diff --git a/lib/friendly_id/base.rb b/lib/friendly_id/base.rb index b0b70561a..5e10762f3 100644 --- a/lib/friendly_id/base.rb +++ b/lib/friendly_id/base.rb @@ -246,5 +246,10 @@ def to_param friendly_id.presence.to_param || super end end + + # Clears slug on duplicate records when calling `dup`. + def dup + super.tap { |duplicate| duplicate.slug = nil } + end end end diff --git a/test/shared.rb b/test/shared.rb index 7c0e7c7a9..fce22ac44 100644 --- a/test/shared.rb +++ b/test/shared.rb @@ -51,6 +51,13 @@ module Slugged end end + test "should set the slug to nil on dup" do + with_instance_of model_class do |record| + record2 = record.dup + assert_nil record2.slug + end + end + test "when validations block save, to_param should return friendly_id rather than nil" do my_model_class = Class.new(model_class) self.class.const_set("Foo", my_model_class)