# How to re-construct ActiveRecord from yaml

**URL:** <https://www.ruby-forum.com/t/how-to-re-construct-activerecord-from-yaml/144554>\
**Category:** Rails\
**Created:** [July 30, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/how-to-re-construct-activerecord-from-yaml/144554 "2008-07-30T21:07:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Do](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@John\_Do](https://www.ruby-forum.com/u/John_Do)\
**Post date:** [July 30, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/how-to-re-construct-activerecord-from-yaml/144554/1 "2008-07-30T21:07:59Z")

</div>

My controller rm\_grants\_controller.rb snip:  
class RmGrantsController \< ApplicationController  
def dump  
File.open(‘db/backup/rm\_grants.yaml’, ‘w’) { |f| f.puts  
RmGrants.find(:all).to\_yaml }  
end  
end

## This creates ‘db/backup/rm\_grants.yaml’ whose content look like

- !ruby/object:RmGrant  
attributes:  
key1: value1  
key2: value2  
key3: value3  
attributes\_cache: {}

- !ruby/object:RmGrant  
attributes:  
key1: value1  
key2: value2  
key3: value3  
attributes\_cache: {}

Now, I want to reconstruct ActiveRecord from the yaml file, so in  
rm\_grants\_controllers.rb  
def load  
records = YAML.load\_file(‘db/backup/rm\_grants.yml’)  
records.each do |record|  
@rm\_grant = RmGrant.new(params[record])  
@rm\_grants.save!  
end  
end

But that doesn’t do what I hope, which is to re-create all the  
records. All it does is create empty records.

puts record.inspect shows  
#\<YAML::Object:0x5d26950 @class=“RmGrant”, @ivars={“attributes\_class”-

> {}, “attributes”=\>{“key1”=\>“value1”, “key2”=\>“value2”,  
> “key3”=\>“value3”}}\>

But I have no idea what this means nor how to decipher it.

---

<div class="post-metadata">

**Author:** ![John\_Do](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@John\_Do](https://www.ruby-forum.com/u/John_Do)\
**Post date:** [July 31, 2008, 2:57pm UTC](https://www.ruby-forum.com/t/how-to-re-construct-activerecord-from-yaml/144554/2 "2008-07-31T14:57:40Z")

</div>

On 30 Jul 2008, at 00:37, John Do wrote:

> * * *
> 
> key2: value2  
> key3: value3  
> attributes\_cache: {}

I’m guessing that this code has been slightly edited from what’s  
actually in your app. Please be careful when you do this as errors  
produced (or omitted) in the process (the code above contains several  
typos and so) can make things very confusing.

I’m guessing you just want to dump the object attributes (ie the  
database attributes). An active record object contains some other  
instance variables that track a bunch of stuff, so you’re not dumping  
quite what you want, so you probably want something like

RmGrant.find(:all).collect {|record| record.attributes}.to\_yaml

> Now, I want to reconstruct ActiveRecord from the yaml file, so in  
> rm\_grants\_controllers.rb  
> def load  
> records = YAML.load\_file(‘db/backup/rm\_grants.yml’)  
> records.each do |record|  
> @rm\_grant = RmGrant.new(params[record])  
> @rm\_grants.save!  
> end  
> end

I’m not entirely sure what you were thinking here. Why are you using  
params at all?  
This should just be @rm\_grant = RmGrant.new(record) (assuming you’ve  
made the change I suggested above)

Fred
