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]) ...... endUnless 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 ||= {} endAnother 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