In this simplified scenario, I have a model called Post
-
Each post has a title and a permalink
-
The permalink is used in the url – i.e. http://mysite.com/blog/permalink
-
Before a post is created, a callback sets the permalink to be the same as the title
I validate the uniqueness of each title but I do not explicitly validate the uniqueness of the permalink for three reasons:
- the permalink is equal to the title, which has already been validated for uniqueness
- users have no concept of the permalink, since they are never asked to enter one, so when they get an error telling them that the permalink needs to be unique, they don’t have a clue what it means
- i’m using mongoid for the ORM and the permalink is the key for each post, and mongoid doesn’t seem to let you modify a key
Now imagine the following scenario:
-
A user creates a post titled “hello” and the url is http://mysite.com/blog/hello
-
The user changes the title to “goodbye”, but the permalink stays the same since it is immutable by design. This is fine, it’s what I want and helps to prevents link-rot.
-
The user then creates a new post, titled “hello” which does not fail the validations since we changed the title of the first post already
The problem here is that we’ve now got two posts with the same url thanks to our before_create callback.
As I said, I don’t want to validate the uniqueness of the permalink as the user in the above scanrio, upon creating the second posts would receive the “permalink must be unique” error and I just know this will confuse them
So I was thinking, is there a validation, or a technique that allows me to prevent a user from creating a post which has a title that is equal to an existing permalink?
If all else fails, then I will just validate the uniqueness of the permalink and write out a really lengthy validation error message that details what the permalink is, and why this one is not deemed to be unique
The stuff we have to think about when making webites, what a carry-on
I can think of four possible solutions:
Validate the uniqueness of permalink like this:
This will present the user with an understandable message.
change your
before_createcallback to create a numbered permalinkchange your
before_createto abefore_savecallback, but than the resulting link is only permanent as long your title doesn’t change.use friendly_id for creating and managing the permalink. It’s very powerfull.