# Loading large amounts of text data in fixtures

**URL:** <https://www.ruby-forum.com/t/loading-large-amounts-of-text-data-in-fixtures/132521>\
**Category:** Rails\
**Created:** [March 21, 2008, 12:28am UTC](https://www.ruby-forum.com/t/loading-large-amounts-of-text-data-in-fixtures/132521 "2008-03-21T00:28:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [March 21, 2008, 12:28am UTC](https://www.ruby-forum.com/t/loading-large-amounts-of-text-data-in-fixtures/132521/1 "2008-03-21T00:28:51Z")

</div>

I am trying to load the contents of XML files into a mediumtext field  
from a fixture.

heres my fixture so far, with some erb to help me out.

\<%  
def load\_owl(name)  
data = File.read("#{RAILS\_ROOT}/spec/fixtures/#{name}\_owl.xml")  
data.gsub!("\n", ‘’)  
“!str #{data}”  
end  
%\>

wine:  
name: Wine  
body: \<%= load\_owl(:wine) %\>

teams:  
name: Teams  
body: \<%= load\_owl(:teams) %\>

pizza:  
name: Pizza  
body: \<%= load\_owl(:pizza) %\>

* * *

This worked for Wine and Teams, but pizza has screwed the pooch. All I  
get is YAML parse errors, which are very hard to pinpoint.

So if anyone has a better idea about how to do a basic YAML escape on a  
something 4000 lines of XML, that would be very useful. Any ideas?

---

<div class="post-metadata">

**Author:** ![squeegy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dec6dc/32.png) [@squeegy](https://www.ruby-forum.com/u/squeegy)\
**Post date:** [March 21, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/loading-large-amounts-of-text-data-in-fixtures/132521/2 "2008-03-21T18:02:00Z")

</div>

Well, I never solved this, but came up with a workaround. Since I don’t  
need these fixtures to have any association linkage I just added a  
method to my spec helper:

def get\_owl(name)  
returning Owl.find\_or\_initialize\_by\_name(name) do |owl|  
if owl.new\_record?  
owl.name = name  
owl.body =  
File.read("#{RAILS\_ROOT}/spec/fixtures/#{name}\_owl.xml")  
owl.save  
end  
end  
end

Seems to work well enough for my needs.

Alex W. wrote:

> I am trying to load the contents of XML files into a mediumtext field  
> from a fixture.
> 
> heres my fixture so far, with some erb to help me out.
> 
> \<%  
> def load\_owl(name)  
> data = File.read("#{RAILS\_ROOT}/spec/fixtures/#{name}\_owl.xml")  
> data.gsub!("\n", ‘’)  
> “!str #{data}”  
> end  
> %\>
> 
> wine:  
> name: Wine  
> body: \<%= load\_owl(:wine) %\>
> 
> teams:  
> name: Teams  
> body: \<%= load\_owl(:teams) %\>
> 
> pizza:  
> name: Pizza  
> body: \<%= load\_owl(:pizza) %\>
