<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>RealityForge.org: Loading Binary Data into Rails Fixtures</title>
    <link>/articles/2006/04/06/loading-binary-data-into-rails-fixtures</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>A little short for a storm trooper</description>
    <item>
      <title>Loading Binary Data into Rails Fixtures</title>
      <description>&lt;p&gt;Loading image data into fixtures was a chore until recently as I had been using separate rake tasks to do the job. The following code demonstrates how easy it is to load binary data such as images into the fixtures via standard mechanisms. It loads some image data from within the &lt;code&gt;$RAILS_ROOT/test/fixtures/&lt;/code&gt; directory and puts it in database.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_yml "&gt;&amp;lt;%
def fixture_data(name)
  render_binary(&amp;quot;#{RAILS_ROOT}/test/fixtures/#{name}&amp;quot;)
end

def render_binary(filename)
  data = File.open(filename,'rb').read
  &amp;quot;!binary | #{[data].pack('m').gsub(/\n/,&amp;quot;\n    &amp;quot;)}\n&amp;quot;
end
%&amp;gt;
picture_data_1:
  id: 1
  picture_id: 1
  content_type: 'image/jpg'
  data: &amp;lt;%= fixture_data(&amp;quot;picture_data_1.jpg&amp;quot;) %&amp;gt;
picture_data_2:
  id: 2
  picture_id: 2
  content_type: 'image/gif'
  data: &amp;lt;%= fixture_data(&amp;quot;picture_data_2.gif&amp;quot;) %&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;If you do not use two spaces as your indent then you will need to alter the line in &lt;code&gt;render_binary(filename)&lt;/code&gt; that replaces newline so that every newline is replaced with two indents.&lt;/p&gt;


	&lt;p&gt;Easy peasy!&lt;/p&gt;


	&lt;p&gt;&lt;b&gt;Update on 16th April 2006&lt;/b&gt;&lt;/p&gt;


	&lt;p&gt;It turns out that it was not as easy peasy under postgres as the driver did not know it had to escape the data as binary as fixtures don&amp;#8217;t actually load the column type. The simplest hack around it is to add in the following bit of code somewhere that just patches the driver if a 0 is in the data. This may not always work but it works with my test data so that is good enough for me at the moment.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter &amp;lt; ActiveRecord::ConnectionAdapters::AbstractAdapter
  def quote(value, column = nil)
    if (value.kind_of?(String) &amp;amp;&amp;amp; column &amp;amp;&amp;amp; column.type == :binary) || (value.kind_of?(String) &amp;amp;&amp;amp; value.include?(0))
      &amp;quot;'#{escape_bytea(value)}'&amp;quot;
    else
      super
    end
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Thu, 06 Apr 2006 07:50:00 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:83791ba6-2051-45ac-9377-76356d4a7717</guid>
      <author>Peter Donald</author>
      <link>http://www.realityforge.org/articles/2006/04/06/loading-binary-data-into-rails-fixtures</link>
      <category>Rails</category>
      <category>rails</category>
      <category>binary</category>
      <category>fixture</category>
      <category>image</category>
      <trackback:ping>http://www.realityforge.org/articles/trackback/44</trackback:ping>
    </item>
    <item>
      <title>"Loading Binary Data into Rails Fixtures" by misunderstood</title>
      <description>&lt;p&gt;Thank you, this seem to work fine.&lt;/p&gt;</description>
      <pubDate>Sat, 08 Apr 2006 00:24:06 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:c99bc012-5691-4405-85eb-2d274e3ca4bb</guid>
      <link>http://www.realityforge.org/articles/2006/04/06/loading-binary-data-into-rails-fixtures#comment-49</link>
    </item>
    <item>
      <title>"Loading Binary Data into Rails Fixtures" by Peter Donald</title>
      <description>&lt;p&gt;Updated entry to use a technique that hopefully works under ruby 1.8.4&lt;/p&gt;</description>
      <pubDate>Fri, 07 Apr 2006 17:35:27 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:03af9d0e-03a7-4b05-a851-5b619620162f</guid>
      <link>http://www.realityforge.org/articles/2006/04/06/loading-binary-data-into-rails-fixtures#comment-48</link>
    </item>
    <item>
      <title>"Loading Binary Data into Rails Fixtures" by misunderstood</title>
      <description>&lt;p&gt;1.8.4&lt;/p&gt;</description>
      <pubDate>Fri, 07 Apr 2006 12:35:12 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:35db54e7-d661-4acb-b36f-ca037a6f0ab4</guid>
      <link>http://www.realityforge.org/articles/2006/04/06/loading-binary-data-into-rails-fixtures#comment-47</link>
    </item>
    <item>
      <title>"Loading Binary Data into Rails Fixtures" by Peter Donald</title>
      <description>&lt;p&gt;It works with the following version of ruby. What version are you running?&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
C:\&amp;gt;ruby --version
ruby 1.8.2 (2004-12-25) [i386-mswin32]
&lt;/pre&gt;
&lt;/code&gt;</description>
      <pubDate>Fri, 07 Apr 2006 08:03:37 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:62591ec7-42c7-4417-9105-336988043977</guid>
      <link>http://www.realityforge.org/articles/2006/04/06/loading-binary-data-into-rails-fixtures#comment-46</link>
    </item>
    <item>
      <title>"Loading Binary Data into Rails Fixtures" by misunderstood</title>
      <description>&lt;p&gt;&lt;span class='caps'&gt;YAML&lt;/span&gt;::Emitter.new({}) 
Syck emitters do not know start_object or end_object.  How did you get this to work?&lt;/p&gt;</description>
      <pubDate>Fri, 07 Apr 2006 07:30:25 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:67875ead-ddcd-453f-9be4-65c0a0c2b7e6</guid>
      <link>http://www.realityforge.org/articles/2006/04/06/loading-binary-data-into-rails-fixtures#comment-45</link>
    </item>
  </channel>
</rss>
