Guest User

Untitled

a guest
May 25th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. require "ostruct"
  2.  
  3. class User < ActiveRecord::Base
  4. serialize :custom
  5.  
  6. def after_initialize
  7. self.custom = OpenStruct.new(custom)
  8. end
  9.  
  10. before_validation :convert_custom_attributes_to_hash
  11. after_validation :convert_custom_attributes_to_ostruct
  12.  
  13. def convert_custom_attributes_to_hash
  14. self.custom = custom.marshal_dump if custom.kind_of? OpenStruct
  15. end
  16.  
  17. def convert_custom_attributes_to_ostruct
  18. self.custom = OpenStruct.new(custom) if custom.kind_of? Hash
  19. end
  20. end
Add Comment
Please, Sign In to add comment