I have a basic Rails model with two properties, name and code. I have validates_uniqueness_of for the code property. However, I'd like to customize the :message to show the name of the duplicate. Is there any way to access this duplicate item?
For example, say I first enter a record called Expired with code EXP. Then I enter Experience with code EXP. I would like the :message to say something like "Code already taken by Expired".
> m1 = Model.new(:name => 'Expired', :code => 'EXP')
> m2 = Model.new(:name => 'Experience', :code => 'EXP')
> m1.save!
> m2.save!
validates_uniqueness_of :code,
:message => "Code already taken by #{duplicate.name}"
Is there any built-in Rails construct that holds the duplicate object so that I can access it like I do in the :message? Or is there any other way I can run code to determine the duplicate when this validation gets triggered?