public
Created

  • Download Gist
my_model.rb
Ruby
1 2 3 4 5
# app/models/my_model.rb
 
class MyModel
attr_accessor :id
end
object.yml
YAML
1 2
--- !ruby/object:MyModel
id: my model
rails console in development environment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
$ rails c
Loading development environment (Rails 3.2.3)
1.9.2p290 :001 > object = YAML.load_file "#{Rails.root}/tmp/object.yml"
ArgumentError: undefined class/module MyModel
from /Users/carol/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:257:in `path2class'
from /Users/carol/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:257:in `resolve_class'
from /Users/carol/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:183:in `visit_Psych_Nodes_Mapping'
from /Users/carol/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych/visitors/visitor.rb:7:in `accept'
from /Users/carol/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych/visitors/to_ruby.rb:16:in `accept'
from /Users/carol/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych/nodes/node.rb:25:in `to_ruby'
from /Users/carol/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:107:in `load'
from /Users/carol/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:205:in `load_file'
from (irb):1
from /Users/carol/.rvm/gems/ruby-1.9.2-p290@railsbug1585/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /Users/carol/.rvm/gems/ruby-1.9.2-p290@railsbug1585/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /Users/carol/.rvm/gems/ruby-1.9.2-p290@railsbug1585/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
rails console in test environment
1 2 3 4
$ RAILS_ENV=test rails c
Loading test environment (Rails 3.2.3)
1.9.2p290 :001 > object = YAML.load_file "#{Rails.root}/tmp/object.yml"
=> #<MyModel:0x00000102b74dc0 @id="my model">
serializing_test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# test/unit/seriralizing_test.rb
 
require 'test_helper'
 
class SerializingTest < Test::Unit::TestCase
def test_yaml_serialize_structure
my_model = MyModel.new
my_model.id = 'my model'
 
File.open( "#{Rails.root}/tmp/object.yml" , 'w' ) do |f|
YAML::dump(my_model, f)
end
 
object = YAML.load_file "#{Rails.root}/tmp/object.yml"
assert( object.instance_of? MyModel ) # passes
assert_equal( 'my model', object.id ) # passes
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.