Skip to content
Andrew Geweke edited this page Dec 19, 2013 · 8 revisions

So, you want to use your first flex column? Here are the steps:

Pick (or add) a column to use as your flex column. This can be called anything (although watch out for the reasonably obvious name of "attributes"; it's reserved by ActiveRecord and weird, weird errors will result). This can be of any textual type (CHAR, VARCHAR, TEXT, CLOB), in which case you'll get pure JSON in your column, or of a binary type (BINARY, VARBINARY, BLOB), in which case you'll get a small header (FC:01,0, or similar) in front of your data, followed by UTF8-encoded JSON. If you're using PostgreSQL, you can also use its native JSON data type, in which case you'll get pure JSON, too.

In the model class for that table, declare a flex column with any fields you want, like so:

class User < ActiveRecord::Base
  flex_column :user_attributes do
    field :foo
    field :bar
  end
end

You're all ready to go. By default, your new fields will happily store any value (scalar, array, Hash, object, whatever) that can be serialized to JSON and parsed from JSON, and you can access your new fields in two ways:

my_user = User.find(...) # or whatever other way of getting a hold of a User object
my_user.foo = "123"      # access directly from the User object
my_user.user_attributes.foo = "123" # or via the `user_attributes` object

When you're ready, here's additional reading on things you may want to do or customize:

See the home page for a complete table of contents.

Clone this wiki locally