Pat Maddox
2006-Apr-03 09:55 UTC
[Rails] Automatically changing strings to pass unique constraints
I have validates_uniqueness_of on one of my model''s attributes. I''d like to make it so that I can set the attribute to something, and if it clashes with an existing record, automatically append a number to it. I''m not really sure of the best way to go about this - any recommendations? Pat
Francois Beausoleil
2006-Apr-03 14:06 UTC
[Rails] Automatically changing strings to pass unique constraints
Hi ! 2006/4/3, Pat Maddox <pergesu@gmail.com>:> I have validates_uniqueness_of on one of my model''s attributes. I''d > like to make it so that I can set the attribute to something, and if > it clashes with an existing record, automatically append a number to > it. I''m not really sure of the best way to go about this - any > recommendations?look into adding a before_validation hook. Do your unique query in there, and append your number, until you are satisfied it''s ok, or you hit a specified limit. http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html#M000786 Hope that helps ! -- Fran?ois Beausoleil http://blog.teksol.info/
Apparently Analagous Threads
- Login_engine - auth against email rather that username?
- Validations based on associations
- Rails 3 Foreign Domain routing - cannot get this to work!
- When adding a record in console, a parameter comes in as null even when I set it
- Am I going too far or Rails is just confusing? was {validates_presence_of *_id attributes}