Tuesday 18 December 2012

Rails attribute serialization

Switching to Postgres after working for over two years on Hbase is quite a change. The ability to use ActiveRecord feels like getting a nuke. Searches, orders, associations, you name it. But on the other hand, one of the things that felt missing was serialization of which I got so accustomed. It just felt wrong to create separate table for users settings. Why not just store it in the User table?!

Well, Active Record has an option for that. Attributes can be marked as serializable and stored in desired format in the database. Default format is YAML, but you can pass Array, Hash or Class of your own. I'll use Hash:
# model
class User < ActiveRecord::Base
  serialize :settings, Hash
end

# controller
def create
  @user = User.new(params[:user])
  ......
end
Unless a value has already been set for the settings attribute, this won't work since its not initialized.
We can fix that with after_initialize callback:
# initialization callback
def after_initialize
  self.settings ||= {} 
end
Another thing we have to do is to make the form add settings parameter around the items we want to set. We are creating a new object with mass assignment so we need to tell the new method that these attributes are part of the settings hash not of the User model. For this this purpose fields_for helper comes in handy:
# form view
<%= f.fields_for :settings do |setting| %>
  Send newsletter <%= setting.check_box :send_newsletter %>
<% end %>
When we save the user and look in the database we should see serialized settings parameter saved as settings: {"send_newsletter"=>"1"} .
I think this is quite useful for avoiding creating all new table,  joins and so on....

No comments:

Post a Comment