diff --git a/lib/friendly_id/base.rb b/lib/friendly_id/base.rb index 013d03daa..4a65ba8e5 100644 --- a/lib/friendly_id/base.rb +++ b/lib/friendly_id/base.rb @@ -262,7 +262,11 @@ def friendly_id # Either the friendly_id, or the numeric id cast to a string. def to_param if friendly_id_config.routes == :friendly - friendly_id.presence.to_param || super + if has_changes_to_save? && (diff = saved_changes[friendly_id_config.query_field] || changes[friendly_id_config.query_field]) + diff.first || diff.second + else + friendly_id.presence.to_param || super + end else super end diff --git a/test/slugged_test.rb b/test/slugged_test.rb index 07b6b0274..54fa6db0c 100644 --- a/test/slugged_test.rb +++ b/test/slugged_test.rb @@ -530,11 +530,12 @@ class Journalist < ActiveRecord::Base transaction do journalist = Journalist.new :name => 'Clark Kent', :active => true assert journalist.save + journalist.reload journalist.name = 'Superman' - journalist.slug = nil + journalist.slug = 'not-so-super-man' journalist.active = nil refute journalist.save - assert_equal 'superman', journalist.to_param + assert_equal 'clark-kent', journalist.to_param end end @@ -543,7 +544,7 @@ class Journalist < ActiveRecord::Base journalist = Journalist.new :name => 'Clark Kent', :active => true assert journalist.save journalist.name = 'x' - journalist.slug = nil + journalist.slug = "no-clark" journalist.active = nil refute journalist.save assert_equal 'clark-kent', journalist.to_param