

Using Serialize Option with ActiveRecord Objects

Documentation seems to be more available on how to build forms with check boxes or a multiple select field for ActiveRecord objects that have a has_many or has_many_and_belongs_to association with other ActiveRecord objects. This article shows you how provide a multiple select form based on a custom defined array, with the selected options stored in a single attribute of your ActiveRecord object.

Lets say you are working on a form for a blog post that needs a multi-select field of statically defined adjectives, with the one or many adjectives saved to one field for the post.

def self.adjectives

Next, inside of your model, insert a line indicating the name of the string or text field you’re going to use to store the serialized values from the form.

class Post < ActiveRecord::Base
  serialize :positive_adjectives, Array

In the view file for your form, insert the following tag to create a select tag which loads all the options with the previously selected ones saved to the post field in a single field, serialized in YAML format.

<%= select_tag 'post[positive_adjectives]', options_for_select(Post.adjectives, @post.positive_adjectives), { :multiple => true, :size => 10 } %>

It appears that there are methods, possibly native ones for Rails 3.2.2 soon, for storing your objects in the database in JSON format instead of YAML.

comments powered by Disqus