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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 On Mon, Jan 9, 2012 at 7:02 AM, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>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, > 3])? > > Thanks in advance > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Jan 8, 2012 at 19:02, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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, > 3])?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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.) On Sun, Jan 8, 2012 at 7:17 PM, Dave Aronson <googlegroups2dave-BRiZGj7G2yRXqviUI+FSNg@public.gmane.org> wrote:> On Sun, Jan 8, 2012 at 19:02, Rodrigo Ruiz <rodrigo.ruiz7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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, >> 3])? > > 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.-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Jan 8, 2012 at 20:38, Paul <paul-mzPrHiy5csbYtjvyW6yDsg@public.gmane.org> wrote:> But, if you knew that you''d never want to do any SQL actions > on that arrayMy crystal ball is rather foggy. Must need a good waxing. ;-)> 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. ;-) -Dave -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.. On Mon, Jan 9, 2012 at 10:17 AM, Dave Aronson < googlegroups2dave-BRiZGj7G2yRXqviUI+FSNg@public.gmane.org> wrote:> On Sun, Jan 8, 2012 at 20:38, Paul <paul-mzPrHiy5csbYtjvyW6yDsg@public.gmane.org> wrote: > > > 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. ;-) > > > 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. ;-) > > -Dave > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.