byroot / activerecord-typedstore

ActiveRecord::Store but with type definition
MIT License
439 stars 57 forks source link

Array types are possible #25

Closed mhenrixon closed 9 years ago

mhenrixon commented 9 years ago

I was using my own hacked together solution until rails 4.2 arrived the file can be found here -> https://gist.github.com/mhenrixon/8fb6f1b8dfbd74d2f734

Allows for 2 custom types to be saved just joining and splitting on a character most likely unused for string and integer arrays

# read value from store
when :string_array then
  value.split('|')
when :int_array then
  value.split('|').map(&:to_i)
# write value to store
value = value.map(&:to_s).join('|') if value.is_a?(Array)

I haven't found a better solution than mine until I stumbled on typedstore. Happy to help if you are interested in supporting arrays as well if not I'll keep working on my own stuff.

byroot commented 9 years ago

What if one of the strings in the array contain a |? You need to escape etc. You might as well serialize to JSON or YAML. And then you can't query it anymore, so you might as well use a blob column.

I prefer not to implement features that would have such limitations. Even more since I beleive you can do it with a custom encoder.

But thanks for the heads up.

mhenrixon commented 9 years ago

No problem at all, I'll just work on my own thing then.