olkarls/has_permalink — Issue #3: fix_duplication and the % wildcard
Author: gmalkas
Created: 2011-10-30 16:43:35+00:00
State: open

Hi,

First of all, thanks for the gem, I love its simplicity.

I just had a weird result today, and looking at the source, it turns out it is a correct behavior, but I find it annoying nonetheless.

I have a Blog model with a title and has_permalink (with the fix_duplication flag set to true) :

```ruby
# encoding: utf-8
class Blog < ActiveRecord::Base
attr_accessible :title, :description

belongs_to :user
has_many :articles

has_permalink :title, true

validates_presence_of :title, :description, :user_id, :permalink
validates_uniqueness_of :permalink

end
```

I first created a blog "Ruby on Rails" which resulted in a __ruby-on-rails__ permalink. But then I created a blog "Ruby", which resulted in a __ruby-1__ permalink. I expected __ruby__. But looking at the code :

```ruby
# Autofix duplication of permalinks
def fix_duplication(permalink)
if auto_fix_duplication
n = self.class.where(["permalink LIKE ?", "#{permalink}%"]).count

if n > 0
resolve_duplication(permalink, n)
else
permalink
end
else
permalink
end
end
```

The % wildcard is responsible for my __ruby-1__ permalink. So, is this what is really intended ?

Is it possible to simply use '=' ?

Thank you for your time.