Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 901413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:33:04+00:00 2026-05-15T15:33:04+00:00

In this simplified scenario, I have a model called Post Each post has a

  • 0

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:

  1. the permalink is equal to the title, which has already been validated for uniqueness
  2. 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
  3. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:33:05+00:00Added an answer on May 15, 2026 at 3:33 pm

    I can think of four possible solutions:

    1. Validate the uniqueness of permalink like this:

      validates_uniqueness_of :permalink, :message => "This title is already taken"
      

      This will present the user with an understandable message.

    2. change your before_create callback to create a numbered permalink

      def what_ever_your_callback_is_named
        self.permalink = self.title.permalinkify # or however you create the permalink
        while self.find_by_permalink(self.permalink)
          # check if it is an already numbered permalink
          if self.permalink =~ /^(.*)(-(\d+))?$/
            self.permalink = "#{$~[1]}-#{$~[3].to_i++}"
          else
            self.permalink << "-2"
          end
        end
      end
      
    3. change your before_create to a before_save callback, but than the resulting link is only permanent as long your title doesn’t change.

    4. use friendly_id for creating and managing the permalink. It’s very powerfull.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple scenario (simplified for the sake of this question): <input type=text
I have a simplified test scenario useful for asking this question: A Product can
A simplified scenario: I have a List<Foo> . Foo has two properties Description (
This is a simplified scenario to cut to the chase: I have one webpage
I have been given a task that can be simplified to this scenario: Customers
My scenario is this: I have a table with a structure like this (simplified)
I have code like this (simplified): def outer(): ctr = 0 def inner(): ctr
I have this (simplified) class: public class StarBuildParams { public int BaseNo { get;
I have a table like this (simplified): ID | Name | Parent --------------------------------- 1
I have a simple (actually simplified :) ) scenario that is possibly the cause

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.