I have been trying to work this out for a while and I just can’t seem to figure out how to do it. I was hoping there would be a value in request.env that I could use, or some obvious way to override the uniqueness validator to accomplish this, but I have not been able to get it.
So when the uniqueness validator fails, I want to be able to capture the id of the record with the duplicate info in the db so I can just use that instead. Has anyone done this?
Instead of messing with a validator, you could use find_or_create_by instead. It’ll use the old object if it already exists, or create a new one if it doesn’t – and it does all the work for you!
Hope this helps!
PS: Here are the Official Rails Docs – the section you want is “Dynamic Attribute-Based Finders”, a little ways down. I just think the first link is more useful…