Saturday, 15 February 2014

serialization - Rails 2.3. validates_uniqueness_of a serialized field -



serialization - Rails 2.3. validates_uniqueness_of a serialized field -

i have field in model serialized , when attempted validates uniqueness of it, doesn't work. (still on rails 2.3 on app)

app/models/foo.rb

class foo < activerecord::base serialize :rules validates_uniqueness_of :rules end

i have attempted store content in hash field instead , validate content hash's uniqueness. ran problem of order of callbacks.

require 'digest/md5' class foo < 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

however, having looked @ active record callbacks order, before_save executed after validation, valid because default value nil , after it's updated new content hash.

http://api.rubyonrails.org/classes/activerecord/callbacks.html

maybe i'm not thinking straight, solution this?

many in advance.

try this:

before_validation :update_content_hash

ruby-on-rails serialization validates-uniqueness-of

No comments:

Post a Comment