Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Rails model Serialization

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 387
    Comment on it

    Rails model Serialization
    Hi Friends,
    Earlier we have discussed about Association in Rails Model. Today we will be covering one more topic related to rails active records, that is Serialization. In summary we can define serialized objects as, So serialized objects (in the context of ActiveRecord) are text/string representations of objects. When serialized, you can save (almost) any Ruby object in a single database field.
    In rails Serialization is done using ActiveModel::Serialization. It does a basic serialization of your object. But first you need to declare attribute hash with the attribute, you wish to serialize. It is must that these attributes are strings. You can represent this in Rails as:

    class Blog
      include ActiveModel::Serialization
     
      attr_accessor :title
    
      def attributes
        {'title' => nil}
      end
    end
    

    For accessing serialized object, rails provide serializable_hash method.

    blog = Blog.new
    blog.serializable_hash   # => {"title"=>nil}
    blog.title = "Association in Rails"
    blog.serializable_hash   # => {"title"=>"Association in Rails"}
    


    Rails provides two serializers for serialization, that are ActiveModel::Serializers::JSON and ActiveModel::Serializers::Xml. The use of these two in a Blog class will be like:

    #For JSON
    class Blog
      include ActiveModel::Serializers::JSON
    
      attr_accessor :title
     
      def attributes
        {'title' => nil}
      end
    end
    
    
    #For JSON
    class Blog
      include ActiveModel::Serializers::Xml
    
      attr_accessor :title
     
      def attributes
        {'title' => nil}
      end
    end
    


    JSON and XML representation of models can be achieved using as_json and to_xml.eg:

    #JSON
    blog = Blog.new
    blog.as_json # => {"title"=>nil}
    blog.title = "Association in Rails"
    blog.as_json # => {"title"=>"Association in Rails"}
    
    #XML
    blog = Blog.new
    blog.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<blog>\n  <title nil=\"true\"/>\n</blog>\n"
    blog.title = "Association in Rails"
    blog.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<blog>\n  <title>Association in Rails</title>\n</blog>\n"
    


    From both JSON and XML string as you define attributes, The method attributes= is used. As:

    class Blog
      include ActiveModel::Serializers::JSON #Will be Xml in case of XML
     
      attr_accessor :title
     
      def attributes=(hash)
        hash.each do |key, value|
          send("#{key}=", value)
        end
      end
     
      def attributes
        {'title' => nil}
      end
    end
    


    At last creating instance of blog and setting value can be done using from_json and from_xml respectively. Example is given below:

    #JSON:
    json = { title: 'Association in Rails' }.to_json
    blog = Blog.new
    blog.from_json(json) # => #<Blog:0x00005c786391290 @title="Association in Rails">
    blog.title           # => "Association in Rails"
    
    
    #Similarly for XML:
    xml = { title: 'Association in Rails' }.to_xml
    blog = Blog.new
    blog.from_xml(xml) # => #<Blog:0x00005c786391290 @title="Association in Rails">
    blog.title         # => "Association in Rails"
    



    Hope you liked this. For more blogs like this, Clik here

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: