Save Array

Hi, is there a way to save an array of float values to my database (for a specific mode, for example User, so it stays like user.values = [0, 2, 54, 3])?

Thanks in advance

Hmm, one way is to implement it as has_many association.

but also you could save it as json or xml. or use serialization http://api.rubyonrails.org/classes/ActiveModel/Serialization.html

Ahmy Yulrizka

There are lots of ways. For instance, if you really want to save it
as an array, *and you don't need to do any math on it*, is to save it
as a string, and turn it into an array again on reload.

However, the *cleanest* (since the above is no doubt making many
people lose their lunch), most *flexible*, and probably even most
*easy to implement* (since Rails would as usual do a lot of the work
for you), would be to make another table. This table would include
the ID(s) of the thing(s) the array is attached to, a positional index
number (not to be confused with a database index), and the value.

For example if you're writing a web site for Immigration and Customs
Enforcement, and you wanted to record what scores an immigrant has
achieved on each of possibly many attempts to pass the citizenship
test, you could use a table called TestAttempts, with immigrant_id,
try_number, and score. This would be a pretty standard one-to-many
relationship, where each Immigrant has_many :test_attempts, and each
Test_Attempt belongs_to :immigrant. (Of course in such a scenario,
you'd probably want more info on there like the date and place of the
test, maybe a more detailed breakdown of the scoring, but that's
another story.)

Or if you're setting up a more general web site, that could include
data about *many* different tests, like a school, you could use
TestAttempts with fields such as student_id, test_id, try_number, and
score. This would be a pretty standard *many*-to-many situation,
where each Student has_many :tests :through => :test_attempts (and
likewise many :scores), and similarly each Test has many :students the
same way (and likewise many :scores).

-Dave

But, if you knew that you'd never want to do any SQL actions on that
array (it was just extra data attached to that record), wouldn't it
execute faster if you just save it as a string? And as far as being
clean, I'd think that not creating a table you didn't need was pretty
clean, too.

(I ask, not because I am convinced that serializing the array is
better, but because I keep wrestling with this every time it comes
up.)

But, if you knew that you'd never want to do any SQL actions
on that array

My crystal ball is rather foggy. Must need a good waxing. :wink:

wouldn't it execute faster if you just save it as a string?

Possibly... but the big question is, which is worth more, the few CPU
milliseconds you save, or the hours of programmer-time to make any
changes?

And as far as being
clean, I'd think that not creating a table you didn't need was pretty
clean, too.

If you're getting overwhelmed with too many tables, chances are your
app is too "enterprisey" anyway. :wink:

-Dave

Yes I think that is possible… after saving the array in the database trying playing arround with this codes…

.enum

.sym

Well, I have a doubt if that will work.

But there is nothing to lose if you try it…