Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Serialized Preferences for Model

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 Rakefile
Octocat-spinner-32 serial_preference.gemspec


If you have a large number of settings/preferences on your model (like a company or a businesss) and you store each preference in a separate model or in separate columns on the model itself, it gets hairy, quickly.

Additionally you require those settings/preferences to be read in from a form and then you need an easy way to validate them too.

Personally, I found that putting settings in the database relationally was hellish.

SerialPreference stores preferences serialized in a hash in your model. All in one place with a DSL to define your settings along with validations and other niceties.

Scratching my own itch.


Add this line to your application's Gemfile:

gem 'serial_preference'

And then execute:

$ bundle

Or install it yourself as:

$ gem install serial_preference


    class Company < ActiveRecord::Base

      include SerialPreference::HasSerialPreferences

      preferences do

        preference :taxable data_type: :boolean, required: true
        preference :vat_no required: false
        preference :max_invoice_items data_type: :integer

        float :rate_of_interest

        # default data type is :string
        # if the preference is required, then a validation is added to the model
        # if the data type is numerical, then a numericality validation is added
        # preferences can be grouped in preference groups

        preference_group "Preferred Ledgers" do
          income_ledger_id data_type: :integer, default: 1

        password field_type: :password


  • Fetching Preference/Groups
    # something you can customize in your form perhaps?
    Company.preference_groups.each do |pg| # => returns an array of preference groups
      # => Name of Preference Group as specified in map e.g. Preferred Ledgers
      # pg.preferences => Array of Preference Definitions
      pg.preferences.each do |preference|
        # preference => PreferenceDefinition
        f.input, required: preference.required?, placeholder: preference.default, as: preference.field_type


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Something went wrong with that request. Please try again.