Using serialized fields in rails

Reading Time: 2 minutes

Hi everybody, in the past days I stored some codes for a model called: Activity. I did this because some activities needs the codes to be completed so the user would have to do the activity and then ask for the code to the admin and then write it in the activity to win the reward of that activity. Now technically there are some requirements for example, a code will always be string because it only can be just numbers or alphanumeric, the activities should have 0 to 20 codes, this means that not all the activities will have a code, of course I could create a model called Code and make a relationship with it and Activity but as far as this is a new feature and we are just testing if the users get to like it or not I decided to use a serialized field in the Activity model to storage all the codes that the activity could have.

What is a serialized field?

But before this. I want to explain what a serialized field is. A serialized field is a text field in the DB that rails serializes to convert it to an array or hash. Some other people have used it to save different kinds of data, but for now I’ll focus on show you how to use it as an array.

Creating the field in the DB

First we need to create the field in the DB to create a migration.

bundle exec rails generate migration AddActivityCodesToActivities codes:text 

Edit the migration like this:

class AddActivityCodesToActivities < ActiveRecord::Migration
  def change
    add_column(:activities, :activity_codes, :text, default: [].to_yaml)
  end
end

Notice that we initialize the field as a serialized array in yaml, if you don’t do it this way you won’t be able to modify it in the future.

Adding the serialized field to the model.

Now let’s add the serialized field to the model, to do that let’s just add this line:

# app/models/activity.rb
serialize :activity_codes, Array

Now rails knows that it has the activity_codes field and that it should be treated as an Array, but you can even add validations to it like this:

# app/models/activity.rb
validates_length_of :activity_codes, maximum: 20, too_long: 'is too long (maximum is 20 codes)'

serialize :activity_codes, Array

Now rails can handle the codes field and also validate its length, how you display it in the view is up to you, in my case the business logic says that I have to display the text box values separated by a comma, that means that I will receive a string with separated values back when the user submit that field in the form, to handle this we can make a simple override of the activity_codes setter, something like this:

# app/models/activity.rb
validates_length_of :activity_codes, maximum: 20, too_long: 'is too long (maximum is 20 codes)'

serialize :activity_codes, Array

def activity_codes=(activity_codes)
    activity_codes = activity_codes.split(',') if activity_codes.is_a?(String)
    super(activity_codes)
  end

And there you go, it doesn’t matter if the form gives you a comma separated value, rails will be capable of dealing with it, that’s all from my part, if you have any doubt or comment I will be happy to read it in the comments’ section.

If you want to know more about MagmaLabs, check it out here!

0 Shares:
You May Also Like