SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    boston
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validate before and after

    I have a 'urlsafe' column in my database. This is a string that gets created off of a company name field aptly called...'name'.

    In my model I have the following:

    Code:
    validates_presence_of :name, :phone, etc.. # make sure all elements are there to save (not urlname though, as this is created by the program)
    validates_uniqueness_of :urlname, :scope => [:city_safe, :address_state] #make sure there are no other businesses with the urlname, in that city, in that state
     before_save :generate_urlname # generate the urlname from the company name
    My problem above comes from the fact I'm stuck wanting to run the first validation to make sure i have a name there, then run the generate_urlname function, and then finally validate the uniquenesss. But since I either have to call the before_save or validate, I can't have both!

    I've looked at all the before_save, after_save, etc calls I could make, but none seem to work. I saw you can create your own validation, but I can't find anywhere that explains how to throw that it doesn't validate if you create your own?

    Thanks for any suggestions.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just validate the uniqueness of the name? If the name is unique then so is the urlname.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    boston
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well there can be lots of McDonalds, and even within one city. I'm leaning towards just writing my own validation in the model. Just scouring the internet now looking for some documentation...

  4. #4
    SitePoint Addict
    Join Date
    May 2004
    Location
    USA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a thought. When you generate_urlname couldn't you tack on the ID as well? You could always strip that out in the display.

    So mcdonalds would be mcdonalds00001. The second one would be mcdonalds00002..... and so forth.

    Just strip out the last 5 characters in the display (or enough that you have room to grow depending on how many you expect to have).

    Of course this is pointless if your goal is to only list ONE mcdonalds per city.

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well your code says: "# generate the urlname from the company name". Suppose your code looks like this:

    Code:
    urlname = "http://#{name}.yoursite.com"
    Now the urlname is unique if and only if the name is unique.

    Am I missing something?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •