Is there any way to save an array to a single column within a database? I have an array of coordinates that are within an array. Thanks in advance! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Is there any way to save an array to a single column within a database? I have an array of coordinates that are within an array. >you can flatten the array into a string via Marshal.dump (and use Marshal.restore to get it back). If you prefer to have "clearer" information in your db, then you could use to_xml or to_yaml, but it will take you a bit more to restore it than using Marshal. regards, javier ramirez -- -------- Estamos de estreno... si necesitas llevar el control de tus gastos visita http://www.gastosgem.com !!Es gratis!! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Alternatively, you could specify the column to be a serialized Array on your model. For example: class Model < ActiveRecord::Base serialize :coordinates, Array end>> Model.create(:coordinates => [[1,2],[3,4]])...>> Model.find(:first, :order => ''id DESC'').coordinates=> [[1,2],[3,4]] In your database, you''ll need to specify ''coordinates'' as :text or :string (the former preferred if you have lots of data). On Mar 21, 8:39 am, Colin Loretz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Is there any way to save an array to a single column within a database? > > I have an array of coordinates that are within an array. > > Thanks in advance! > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks I will give it a shot. javier ramirez wrote:>> Is there any way to save an array to a single column within a database? I have an array of coordinates that are within an array. >> > you can flatten the array into a string via Marshal.dump (and use > Marshal.restore to get it back). If you prefer to have "clearer" > information in your db, then you could use to_xml or to_yaml, but it > will take you a bit more to restore it than using Marshal. > > regards, > > javier ramirez > > -- > -------- > Estamos de estreno... si necesitas llevar el control de tus gastos > visita http://www.gastosgem.com !!Es gratis!!-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This one likes like a solid plan. Thanks a lot. Eden Li wrote:> Alternatively, you could specify the column to be a serialized Array > on your model. For example: > > class Model < ActiveRecord::Base > serialize :coordinates, Array > end >-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---