0

I have a field that is stored like this:

serialize :value

And, I'd like to store it as an array/hash/string depending on the input format. Is there an accepted best practice about how to handle this? The input will always come from the user.

Geo
  • 93,257
  • 117
  • 344
  • 520

2 Answers2

1

If I understand your problem correctly, you are talking about the opposite of serialization, which is parsing or tokenzing (converting a string into meaningful pieces)... That's a very generic problem, but take a look at the String.match and String.scan to get you started. Here are some stack overflow quetions that could be helpful (remember to search for things like this first!)

What's the "ruby way" to parse a string for a single key/value?

How do I tokenize this string in Ruby?

Community
  • 1
  • 1
Steve Benner
  • 1,679
  • 22
  • 26
  • Sure, this is exactly what I had in mind, but before I started to parse away at strings and hashes, I thought that maybe this had been tackled before :) – Geo Sep 03 '13 at 07:49
  • To be helpful, its really important to put more information about the context of your problem up in the question. Without knowing your domain it's hard to help. But think hardly about the meaning of your data. Good practice is to store data in an **array** if it is a list of stand-alone values (think CSV) that don't rely on a different set of values to make sense, and in a **hash** if the information is relational at all. – Steve Benner Sep 03 '13 at 08:19
0

The best practice is to serialize Arrays and Hashes. When it comes to string there is no need for serialization. HTML form elements like checkboxes posts the array, which you can be serialized and saved directly in table. For Hashes, you should manipulate yourself accordingly and apply to the attribute.

techvineet
  • 5,041
  • 2
  • 30
  • 28