  
<div id="snippet_432" class="snippet">
  <h2><a href="/snippets/432-How-to-get-ActiveRecord-and-Rails-to-print-SQL-to-the-production-log" title="How to get ActiveRecord and Rails to print SQL to the production log - Ruby - sql, production, activerecord, rails, logging">How to get ActiveRecord and Rails to print SQL to the production log</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/124-Ruby">
      Ruby</a> posted 28 days ago by christian
          
  </div>

  <div class="body">
    <p>Add to the end of config/environment.rb:<br />
<pre class="active4d"><span class="line-numbers">   1 </span> <span class="LibraryClassType">ActiveRecord</span>::<span class="FunctionName">Base</span>.<span class="FunctionName">logger</span>.<span class="FunctionName">level</span> <span class="Operator">=</span> <span class="LibraryClassType">Logger</span>::<span class="FunctionName">DEBUG</span>
</pre></p>
<p>Config/environments/production.rb might also work.</p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/255-sql">sql</a>, <a href="/tags/106-production">production</a>, <a href="/tags/490-activerecord">activerecord</a>, <a href="/tags/9-rails">rails</a>, <a href="/tags/295-logging">logging</a>
    
    
  </div>
</div>



  
<div id="snippet_420" class="snippet">
  <h2><a href="/snippets/420-How-to-customize-Hirb-output" title="How to customize Hirb output - Ruby - hirb, irb, console, rails">How to customize Hirb output</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/124-Ruby">
      Ruby</a> posted 3 months ago by christian
          
  </div>

  <div class="body">
    <p>Only print id, created_at and title for FeedItem class:<br />
<pre class="active4d"><span class="line-numbers">   1 </span> <span class="LibraryClassType">Hirb</span>.<span class="FunctionName">disable</span>
<span class="line-numbers">   2 </span>   <span class="LibraryClassType">Hirb</span>.<span class="FunctionName">enable</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>output</span> =&gt; {
<span class="line-numbers">   3 </span>     <span class="String"><span class="String">&quot;</span>FeedItem<span class="String">&quot;</span></span>=&gt;{
<span class="line-numbers">   4 </span>       <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>options</span>=&gt;{
<span class="line-numbers">   5 </span>         <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>fields</span>=&gt;<span class="String"><span class="String">%w{</span>id created_at title<span class="String">}</span></span>
<span class="line-numbers">   6 </span>       }
<span class="line-numbers">   7 </span>     }
<span class="line-numbers">   8 </span>   }
</pre></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/872-hirb">hirb</a>, <a href="/tags/278-irb">irb</a>, <a href="/tags/134-console">console</a>, <a href="/tags/9-rails">rails</a>
    
    
  </div>
</div>



  
<div id="snippet_401" class="snippet">
  <h2><a href="/snippets/401-How-to-seed-your-database-with-JSON-YAML-data-in-Rails-2-3-4" title="How to seed your database with JSON/YAML data in Rails 2.3.4 - Ruby - seed, rails, yaml, json, sql, bootstrap, database">How to seed your database with JSON/YAML data in Rails 2.3.4</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/124-Ruby">
      Ruby</a> posted 4 months ago by christian
          
  </div>

  <div class="body">
    <p>In Rails 2.3.4 there&#8217;s a new rake task called <strong>db:seed</strong> that allows you to seed your database with data.  To use it you first need a file called db/seed.rb. In it put the code that creates the ActiveRecord objects your application needs.</p>
<p>You can load and execute <span class="caps">SQL</span> from an external file, or plain Ruby objects from <span class="caps">YAML</span> or <span class="caps">JSON</span> as I&#8217;ll show you in this example.</p>
<p>Put this in db/seed.rb:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> json <span class="Operator">=</span> <span class="LibraryClassType">ActiveSupport</span>::<span class="FunctionName">JSON</span>.<span class="FunctionName">decode</span>(<span class="LibraryClassType">File</span>.<span class="FunctionName">read</span>(<span class="String"><span class="String">'</span>db/seeds/countries.json<span class="String">'</span></span>))
<span class="line-numbers">   2 </span> 
<span class="line-numbers">   3 </span> json.<span class="FunctionName">each</span> <span class="Keyword">do </span>|<span class="Variable">a</span>|
<span class="line-numbers">   4 </span>   <span class="LibraryClassType">Country</span>.<span class="FunctionName">create!</span>(a[<span class="String"><span class="String">'</span>country<span class="String">'</span></span>])
<span class="line-numbers">   5 </span> <span class="Keyword">end</span>
</pre></p>
<p>Next export the data from an existing database by running the following command in the Rails console:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> <span class="LibraryClassType">File</span>.<span class="FunctionName">open</span>(<span class="String"><span class="String">'</span>db/seeds/countries.json<span class="String">'</span></span>, <span class="String"><span class="String">'</span>w<span class="String">'</span></span>) { |<span class="Variable">f</span>| f <span class="Operator">&lt;&lt;</span> <span class="LibraryClassType">Country</span>.<span class="FunctionName">all</span>.<span class="FunctionName">to_json</span> }
</pre></p>
<p>Now when you run <strong>rake db:seed</strong>, Rails will load the countries.json file and insert the data into your database.</p>
<h2>Pretty print <span class="caps">JSON</span> data</h2>
<p>You&#8217;ll probably want to pretty print your data if you&#8217;re going to manually edit it. This can be done by following the instructions in <a href="http://snippets.aktagon.com/snippets/412-How-to-pretty-print-JSON-data-with-Ruby">this snippet</a></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/847-seed">seed</a>, <a href="/tags/9-rails">rails</a>, <a href="/tags/568-yaml">yaml</a>, <a href="/tags/848-json">json</a>, <a href="/tags/255-sql">sql</a>, <a href="/tags/703-bootstrap">bootstrap</a>, <a href="/tags/709-database">database</a>
    
    
  </div>
</div>



  
<div id="snippet_388" class="snippet">
  <h2><a href="/snippets/388-How-to-fix-issues-with-missing-gem-specifications" title="How to fix issues with missing gem specifications - Shell Script (Bash) - hpricot, gem, unpack, rails">How to fix issues with missing gem specifications</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/131-Shell-Script-Bash-">
      Shell Script (Bash)</a> posted 5 months ago by christian
          
  </div>

  <div class="body">
    <p>I was getting this error after unpacking hpricot with <strong>gem unpack hpricot</strong>. I also tried <strong>rake gems:unpack hpricot</strong> but it did nothing&#8230;<br />
<pre class="active4d"><span class="line-numbers">   1 </span> config.gem: Unpacked gem hpricot-0.8.1 <span class="Keyword">in</span> vendor/gems has no specification file. Run <span class="String"><span class="String">'</span>rake gems:refresh_specs<span class="String">'</span></span> to fix this. 
</pre></p>
<p>The rake gems:refresh_specs command doesn&#8217;t work, and appears to have been a temporary workaround, so to fix this error I did this:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> cd vendor/gems/hpricot-0.8.1
<span class="line-numbers">   2 </span> gem specification hpricot <span class="Operator">&gt;</span> .specification
</pre></p>
<p>I had this issue with Rails 2.3.4.</p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/366-hpricot">hpricot</a>, <a href="/tags/325-gem">gem</a>, <a href="/tags/826-unpack">unpack</a>, <a href="/tags/9-rails">rails</a>
    
    
  </div>
</div>



  
<div id="snippet_384" class="snippet">
  <h2><a href="/snippets/384-How-to-customize-to-json" title="How to customize to_json - Ruby - to_json, activerecord, rails">How to customize to_json</a></h2>
  <div class="details">
    <a style="background-color: #FFFF94;" href="http://snippets.aktagon.com/languages/124-Ruby">
      Ruby</a> posted 5 months ago by christian
          
  </div>

  <div class="body">
    <p><pre class="active4d"><span class="line-numbers">   1 </span> <span class="Keyword">def</span> <span class="FunctionName">to_json</span>(<span class="FunctionArgument">options <span class="Operator">=</span> <span class="FunctionArgument">{</span><span class="FunctionArgument">}</span></span>)
<span class="line-numbers">   2 </span>   <span class="Keyword">if</span> options.<span class="FunctionName">empty?</span>
<span class="line-numbers">   3 </span>     <span class="Keyword">super</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>only</span> =&gt; [<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>id</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span>]
<span class="line-numbers">   4 </span>   <span class="Keyword">else</span>
<span class="line-numbers">   5 </span>     <span class="Keyword">super</span> options
<span class="line-numbers">   6 </span>   <span class="Keyword">end</span>
<span class="line-numbers">   7 </span> <span class="Keyword">end</span>
<span class="line-numbers">   8 </span> 
</pre></p>
<p>Now post.to_json will only include the id and name attributes.</p>
<p>Note that for arrays of objects&#8212;at least with Rails 2.3.4&#8212;you need use the same parameters on the array.to_json method:</p>
<p><pre class="active4d"><span class="line-numbers">   1 </span> <span class="LibraryClassType">Post</span>.<span class="FunctionName">all</span>.<span class="FunctionName">to_json</span> <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>only</span> =&gt; [<span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>id</span>, <span class="UserDefinedConstant"><span class="UserDefinedConstant">:</span>name</span>]
</pre></p>
  </div>

  <div style="font-size: 0.8em;margin:0.5em;">
    
      Tagged <a href="/tags/819-to-json">to_json</a>, <a href="/tags/490-activerecord">activerecord</a>, <a href="/tags/9-rails">rails</a>
    
    
  </div>
</div>




<div class="pagination"><span class="disabled">&laquo; Previous</span> <span class="current">1</span> <a href="/tags/show/9-rails/page/2">2</a> <a href="/tags/show/9-rails/page/3">3</a> <a href="/tags/show/9-rails/page/4">4</a> <a href="/tags/show/9-rails/page/5">5</a> <a href="/tags/show/9-rails/page/6">6</a> <a href="/tags/show/9-rails/page/7">7</a> <a href="/tags/show/9-rails/page/8">8</a> <a href="/tags/show/9-rails/page/9">9</a> <a href="/tags/show/9-rails/page/10">10</a> <a href="/tags/show/9-rails/page/11">11</a> <a href="/tags/show/9-rails/page/2">Next &raquo;</a></div>

