title: Rails 2.3. validates_uniqueness_of a serialized field
body: <p>I have a field in a model that is serialized and when I attempted to validates the uniqueness of it, it doesn't work. (Still on Rails 2.3 on this app)</p>
  
  <p>app/models/foo.rb</p>
  
  <pre><code>class foo &lt; ActiveRecord::Base
  
     serialize :rules
  
     validates_uniqueness_of :rules
  
  end
  </code></pre>
  
  <p>I have attempted to store the content in a hash field instead and validate the content hash's uniqueness. Then I ran to another problem of the order of the callbacks.</p>
  
  <pre><code>require 'digest/md5'
  
  class foo &lt; ActiveRecord::Base
  
     before_save :update_content_hash
  
     validates_uniqueness_of :content_hash
  
     def update_content_hash
        self.content_hash = OpenSSL::Digest::SHA1.hexdigest(self.rules.flatten)
  
     end
  end
  </code></pre>
  
  <p>However, having looked at the Active Record callbacks order, before_save is executed <em>after</em> validation, so it will always be valid because the default value is nil and after that it's updated to the new content hash.</p>
  
  <p><a href="http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html" rel="nofollow">http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html</a></p>
  
  <p>Maybe I'm not thinking straight, any solution to this? </p>
  
  <p>Many thanks in advance.</p>