Rails 4.0 Sneak Peek: PostgreSQL array support

I’m happy to announce that Rails 4.0 now has support for PostgreSQL
arrays. You can create an
array column easily at the time of migration by adding :array => true.
Creating an array column will respect the other options you add to the
column (length, default, etc).

It should be noted when setting the default value for an array column,
you should use PostgreSQL’s array notation for that value
({value,another value}). If you want the default value to be an empty
array you would have :default => '{}'.

An example of a model with an array value

Let’s say that we have a user model, which has a formal first and last
name, and also a number of nicknames (I rarely ever go by Daniel). The
following code would create the table we need to store this.

If we then retrieved this record from the database, the nicknames
value would be casted to an array, instead of returning the string of
{Jack,Johnny}. Rails 4.0 has a pure ruby array value parser, but if
you would like to speed up the parsing process, the previously mentioned
pg_array_parserr)
gem will be used if it is available. PgArrayParser has
a C extension, and a Java implementation for JRuby (although the gem
currently broken in JRuby, this is something I am fixing now).

One important thing to note when interacting with array (or other
mutable values) on a model. ActiveRecord does not currently track
“destructive”, or in place changes. These include array pushing and
poping, advance-ing DateTime objects. If you want to use a
“destructive” update, you must call <attribute>_will_change! to let
ActiveRecord know you changed that value. With our User model, if we
wanted to append a nickname, you can do the following:

One last note about arrays in PostgreSQL: there are no element count
constraints, and any array can be multidimensional. With the
multidimensional arrays, they must be “square” (the sub arrays must all
have the same number of elements).

[[1,2,3], [2,3,4], [4,5,nil]]
# Valid array value in PostgreSQL, each subarray has the same number of
# elements
[1,2,[3,4]]
# Invalid array, we are mixing values and arrays at a single level

In my next article, I will talk about querying PostgreSQL arrays in both
postgres_ext and Rails 4.0. Go forth and use arrays in Rails 4.0!