On Sun, Jan 8, 2012 at 19:02, Rodrigo Ruiz wrote:
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,
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
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 Aronson, President, Dave Aronson Software Engineering and Training
Ruby on Rails Freelancing (Northern Virginia, Washington DC, or Remote)
DaveAronson.com, Codosaur.us, Dare2XL.com, & RecruitingRants.com (NEW!)
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (me)
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to email@example.com.
To unsubscribe from this group, send email to firstname.lastname@example.org.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.