Created
May 2, 2012 02:19
-
-
Save carols10cents/2573092 to your computer and use it in GitHub Desktop.
Rails Issue #1585/SO question http://stackoverflow.com/questions/4705867/rails-doesnt-load-classes-on-deserializing-yaml-marshal-objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/my_model.rb | |
class MyModel | |
attr_accessor :id | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- !ruby/object:MyModel | |
id: my model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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>' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment