I have create a yaml object via: h = { "name" => "scott"} y = h.to_yaml If I have a "text" column type in mysql can I just assign the result of "to_yaml" to my "text" column or am I mssing a step? thanks, scott. -- Scott F. Walter Scott F. Walter Principal Consultant Vivare, Inc. E: scott.walter-uosFFu51klvQT0dZR+AlfA@public.gmane.org E: scott-APWf0AbNa2kIjDr1QQGPvw@public.gmane.org Visit scottwalter.com <http://scottwalter.com> --Point. Click. Explore!
"Scott F. Walter" <scott.walter-uosFFu51klvQT0dZR+AlfA@public.gmane.org> writes:> I have create a yaml object via: > > h = { "name" => "scott"} > y = h.to_yaml >I do this to store a "standard" Ruby object on an AR object. The reason is mainly about testing. In this example the ARObject is the ActiveRecord object that contains another object. The yaml_object is the standard object that gets stored in the database. class ARObject < ActiveRecord::Base attr_accessor :yaml_object before_save :save_yaml_object def save_yaml_object self.expression = self.yaml_object.dump end def yaml_object Yaml_Object.load( self.expression) end def yaml_object=(yaml_object) self.expression = yaml_object.dump end end This gives us methods @arobj.yaml_object to access the real Ruby object and @arobj.yaml_object= to assign the Ruby object. Here''s some tests that demonstrate how to use it: class ARObjectTest < Test::Unit::TestCase fixtures :ar_objects def test_new yaml_object = YamlObject.new("some parameters") arobj = ARObject.new arobj.yaml_object = yaml_object assert arobj.save assert arobj.reload assert arobj.yaml_object.returns_true end end The thing to note is that YamlObject is defined elsewhere with a method YamlObject#returns_true that does what it says. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
On Tue, 2005-09-06 at 16:32, Doug Alcorn wrote:> I do this to store a "standard" Ruby object on an AR object. The > reason is mainly about testing. In this example the ARObject is the > ActiveRecord object that contains another object. The yaml_object is > the standard object that gets stored in the database.I needed to store "standard" Ruby objects intermixed with AR objects in such a way that I got back object linked in the same way they had been when stored (e.g., a graph containing multiple links to the same AR object shouldn''t reload with a bunch of distinct but identically initialized objects). Write-up here: http://wiki.rubyonrails.com/rails/show/HowToUsePolymorphicColumns --MarkusQ