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!
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!
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!!
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 L. [email protected]
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
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!!
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs