# Slurp or dump a table to yml

**URL:** <https://www.ruby-forum.com/t/slurp-or-dump-a-table-to-yml/138890>\
**Category:** Rails\
**Created:** [May 28, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/slurp-or-dump-a-table-to-yml/138890 "2008-05-28T17:52:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 28, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/slurp-or-dump-a-table-to-yml/138890/1 "2008-05-28T17:52:32Z")

</div>

I’m looking for a way to slurp or dump a table to a yml file.

I have found several ‘backup’ procedures but they cause problems with my  
join tables and I just want to dump a couple of the join tables anyway.

Is there some easy way to do this?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 28, 2008, 8:32pm UTC](https://www.ruby-forum.com/t/slurp-or-dump-a-table-to-yml/138890/2 "2008-05-28T20:32:57Z")

</div>

I just tried this in script/console:

$ ./script/console  
Loading development environment (Rails 2.0.2)

> > File.open “users.txt”, “w” do |file|  
> > ?\> User.all.each do |user|  
> > ?\> file.write user.to\_yaml
> 
> > end  
> > end  
> > =\> […bunch of users…]
> 
> > exit  
> > $ ls  
> > Capfile app/ data/ doc/ lib/ public/  
> > spec/ test/ users.txt  
> > Rakefile config/ db/ flex/ log/ script/  
> > stories/ tmp/ vendor/  
> > $

The file was created at the root of my rails app. I don’t know if  
there are better ways to do it, but that was certainly quick.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 28, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/slurp-or-dump-a-table-to-yml/138890/3 "2008-05-28T20:41:50Z")

</div>

On Wed, 2008-05-28 at 14:32 -0400, Craig D. wrote:

> > > exit  
> > > $ ls  
> > > Capfile app/ data/ doc/ lib/ public/  
> > > spec/ test/ users.txt  
> > > Rakefile config/ db/ flex/ log/ script/  
> > > stories/ tmp/ vendor/  
> > > $
> 
> The file was created at the root of my rails app. I don’t know if  
> there are better ways to do it, but that was certainly quick.

* * *

gotcha…nice…thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 30, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/slurp-or-dump-a-table-to-yml/138890/4 "2008-05-30T18:24:45Z")

</div>

On Wed, 2008-05-28 at 14:32 -0400, Craig D. wrote:

> > > exit  
> > > $ ls  
> > > Capfile app/ data/ doc/ lib/ public/  
> > > spec/ test/ users.txt  
> > > Rakefile config/ db/ flex/ log/ script/  
> > > stories/ tmp/ vendor/  
> > > $
> 
> The file was created at the root of my rails app. I don’t know if  
> there are better ways to do it, but that was certainly quick.

* * *

this works except for one thing…each record is written like this…

— !ruby/object:Right  
attributes:  
name: Personnel Admin  
action: index  
id: “1”  
controller: assessments  
— !ruby/object:Right  
attributes:  
name: Personnel Admin  
action: find  
id: “2”  
controller: assessments

so each item has the same identifier and you cannot use it without  
modification. I suppose I could figure out a way with sed but this  
doesn’t quite work as well as I had hoped.

Thanks

Craig
