3

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-separated 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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mattias
  • 33
  • 1
  • 3

2 Answers2

3

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
  • 8,464
  • 3
  • 41
  • 58
2

You can use serialize with a custom coder in rails 3.1.

See my answer to this question. :-)

Community
  • 1
  • 1
balu
  • 3,619
  • 1
  • 25
  • 18