views:

27

answers:

1

Hi there,

I'm looking for a way to store a serialized value of eg. IDs in a column. In before claims that this is not an optimal design: the column is used for IDs of associated records, but will only be used when displaying the record - so no queries are made with selection on the column and no joins will be made on this column either.

In Rails I can serialize the column by using:

class Activity
   serialize :data
end

This encodes the column as YAML. For legacy sake and since I'm only storing one dimensional arrays containing only integers, I find it more suitable to store it as a comma-seperated value.

I've successfully implemented basic accessors like this:

def data=(ids)
    ids = ids.join(",") if ids.is_a?(Array)
    write_attribute(:data, ids)
end

def data
    (read_attribute(:data) || "").split(",")
end

This works pretty fine. However I'd like to add array-like methods to this attribute:

activity = Activity.first
activity.data << 42
...

How would I do this?

Thanks!

A: 

You can do it with composed_of feature as explained in this post. It should be something like:

  composed_of :data, :class_name => 'Array', :mapping => %w(data to_csv),
                   :constructor => Proc.new {|column| column.to_csv},
                   :converter   => Proc.new {|column| column.to_csv}

  after_validation do |u|
    u.data = u.data if u.data.dirty? # Force to serialize
  end

Haven't tested it though.

Vlad Zloteanu
Thanks for the link - that's what I was missing :-)
Mattias