Defining Model Preferences
To define a model preference, you need to add them to your model class. Make sure to generate a migration to add thepreferences column to the table. This column will store the preferences in a serialized format.
server/app/models/spree/brand.rb
Spree.base_class can declare preferences — the reader and writer methods come with it, so there is nothing to include. The values live in that model’s preferences column.
Accessing Model Preferences
Once preferences have been defined for a model, they can be accessed either using the shortcut methods that are generated for each preference or the generic methods that are not specific to a particular preference.Shortcut Methods
There are several shortcut methods that are generated. They are shown below. Query methods:Remember to run
brand.save after setting the preference value to save the changes to the database.Generic Methods
When the preference name is only known at runtime, read and write it generically:Accessing All Preferences
You can get a hash of all stored preferences by accessing thepreferences helper:
Models with preferences
Around fifty models use preferences, and the pattern is always the same: a family of subclasses that share a table but need different settings.
This is why preferences exist rather than columns:
Spree::Calculator::FlatRate and Spree::Calculator::TieredPercent are rows in one table with entirely different settings, and neither wants a column the other leaves null.
Spree::Theme

