# Calling to\_yaml on an array of ActiveRecord objects is producing TypeError: wrong argument type nil

**URL:** <https://www.ruby-forum.com/t/calling-to-yaml-on-an-array-of-activerecord-objects-is-producing-typeerror-wrong-argument-type-nil/135540>\
**Category:** Rails\
**Created:** [April 22, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/calling-to-yaml-on-an-array-of-activerecord-objects-is-producing-typeerror-wrong-argument-type-nil/135540 "2008-04-22T23:29:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [April 22, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/calling-to-yaml-on-an-array-of-activerecord-objects-is-producing-typeerror-wrong-argument-type-nil/135540/1 "2008-04-22T23:29:13Z")

</div>

I have a query I am running from the console that is eager loading the  
address on the people model:

people = People.find(:all,  
:include =\> [:address],  
:limit =\> 50)

Trying to output that to yaml via:

people.to\_yaml

Produces the following exception:

TypeError: wrong argument type nil (expected Data)  
from /usr/local/lib/ruby/1.8/yaml.rb:387:in `emit' from /usr/local/lib/ruby/1.8/yaml.rb:387:in`quick\_emit’  
from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:107:in `to\_yaml’  
from (irb):10

I don’t see any nil data in the include… thoughts?

Thanks,

Scott

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [June 16, 2010, 10:33pm UTC](https://www.ruby-forum.com/t/calling-to-yaml-on-an-array-of-activerecord-objects-is-producing-typeerror-wrong-argument-type-nil/135540/2 "2010-06-16T22:33:55Z")

</div>

I also run into the same error when running Spec tests that trigger  
Observers, which trigger DelayedJobs.

The full stack trace:

ruby-1.8.7-p249/lib/ruby/1.8/yaml.rb:391:in `emit' ruby-1.8.7-p249/lib/ruby/1.8/yaml.rb:391:in`quick\_emit’  
ruby-1.8.7-p249/lib/ruby/1.8/yaml/rubytypes.rb:15:in `to_yaml' vendor/bundler_gems/gems/delayed_job-2.0.3/lib/delayed/backend/base.rb:61:in`payload\_object=’  
vendor/bundler\_gems/gems/activerecord-2.3.5/lib/active\_record/base.rb:2746:in  
`send' vendor/bundler_gems/gems/activerecord-2.3.5/lib/active_record/base.rb:2746:in`attributes=’  
vendor/bundler\_gems/gems/activerecord-2.3.5/lib/active\_record/base.rb:2742:in  
`each' vendor/bundler_gems/gems/activerecord-2.3.5/lib/active_record/base.rb:2742:in`attributes=’  
vendor/bundler\_gems/gems/activerecord-2.3.5/lib/active\_record/base.rb:2438:in  
`initialize' vendor/bundler_gems/gems/activerecord-2.3.5/lib/active_record/base.rb:721:in`new’  
vendor/bundler\_gems/gems/activerecord-2.3.5/lib/active\_record/base.rb:721:in  
`create' vendor/bundler_gems/gems/delayed_job-2.0.3/lib/delayed/backend/base.rb:21:in`enqueue’  
app/models/mail\_opt\_in\_observer.rb:12:in `after_save' app/models/mail_opt_in_observer.rb:11:in`each’  
app/models/mail\_opt\_in\_observer.rb:11:in `after_save' vendor/plugins/state_machine/lib/state_machine/integrations/active_record/observer.rb:32:in`send’  
vendor/plugins/state\_machine/lib/state\_machine/integrations/active\_record/observer.rb:32:in  
`update’

I’ve been trying to find the root of this for a few days now and its  
getting a little frustrating. I’ve tried to re-open the class and do the  
following (because I noticed the error doesn’t occur when I _clone_ the  
object and then call to\_yaml).

module ActiveRecord  
class Base

```
to_old_yaml = self.instance_method(:to_yaml)

define_method(:to_yaml) do
  clone = self.clone
 clone.id = self.id
  oh_boy = to_old_yaml.bind(clone).call
 #debugger
 #testtest = 'test'
  return oh_boy
end

```

end  
end

…but this yields a similar error:

wrong argument type String (expected Data)

> Scott wrote:  
> I have a query I am running from the console that is eager loading the  
> address on the people model:
> 
> people = People.find(:all,  
> :include =\> [:address],  
> :limit =\> 50)
> 
> Trying to output that to yaml via:
> 
> people.to\_yaml
> 
> Produces the following exception:
> 
> TypeError: wrong argument type nil (expected Data)  
> from /usr/local/lib/ruby/1.8/yaml.rb:387:in `emit' from /usr/local/lib/ruby/1.8/yaml.rb:387:in`quick\_emit’  
> from /usr/local/lib/ruby/1.8/yaml/rubytypes.rb:107:in `to\_yaml’  
> from (irb):10
> 
> I don’t see any nil data in the include… thoughts?
> 
> Thanks,
> 
> Scott
