Web Development

In the previous article, How to Add a Blob using Rails ActiveRecord, we needed to use a blob to store large data. We added a blob data type via the migration file.

class AddMonthsToNyC240 < ActiveRecord::Migration
  def self.up
    add_column :ny_c240s, :weeks52, :binary, :limit => 10.megabyte
  def self.down
    remove_column :ny_c240s, :weeks52 end

We migrated the file:

== 20161029155357 AddMonthsToNyC240: migrating ================================
-- add_column(:ny_c240s, :weeks52, :binary, {:limit=>10485760})
   -> 0.1093s
== 20161029155357 AddMonthsToNyC240: migrated (0.1095s) =======================

Now we need to add some functions to the ActiveRecord Model

class NyC240 < ActiveRecord::Base
  serialize :weeks52

Voila! You can now save data here and ActiveRecord will automatically know if it is a Hash or Array.