<?xml version="1.0" encoding="UTF-8"?>
<article>
  <content>&lt;p&gt;Oracle database software&amp;rsquo;s near-zero cost in academia result in ubiquitous usage. Pairing Rails with Oracle isn&amp;rsquo;t presently void of issues, but there are no real show stoppers. Here are a few things I&amp;rsquo;ve ran into and how I worked around them.&lt;br /&gt;Oracle&amp;rsquo;s Missing Time Field&lt;br /&gt;Oracle doesn&amp;rsquo;t have a time field like other popular databases. You may very well define a :time type in an ActiveRecord::Migration, but Oracle will give you a date field instead. The date field will hold a time value most of the time, but not always.&lt;br /&gt;A sort of &amp;ldquo;gotcha&amp;rdquo; occurs when you get a record with the time portion set to midnight. In this special case Oracle doesn&amp;rsquo;t return any time value at all in this special case, instead it just returns the date part of the record. On the Ruby/Rails end you get back a Date class instead of a Time class. This means your time field will not have min or hour methods. The solution is coercion, using to_time:&lt;br /&gt;foo = Foo.find( :first )&lt;br /&gt;foo.my_time_field = foo.my_time_field.to_time&lt;br /&gt;After that your date field will contain a time that&amp;rsquo;s set to midnight (luckily this matches our special case from above exactly). Another workaround is to rescue the exception with a zero value:&lt;br /&gt;foo.my_time_field.hour rescue 0&lt;br /&gt;foo.my_time_field.min rescue 0&lt;br /&gt;Keep session data stored in Oracle simple&lt;br /&gt;The Oracle-Ruby database driver has some issues with storing complex data in a session. I had a scenario where I needed to store objects in a session temporarily while waiting to put their parent data in the database first. Something like this would cause the issue:&lt;br /&gt;foo = Foo.new( params[:foo] )&lt;br /&gt;session[:foos] &amp;lt;&amp;lt; foo&lt;br /&gt;I suspect the cause is the fact that an ActiveRecord object owns a database handle, and when you accumulate several of them, threading issues arise. I will admit I did not pursue it further once I found a workaround.&lt;br /&gt;The solution was to not store the complex data in the session at all, but instead only store a session variable pointing to the data:&lt;br /&gt;foo = Foo.create( params[:foo] )&lt;br /&gt;session[:foos] &amp;lt;&amp;lt; foo.id&lt;br /&gt;This solution works fine but has the side effect of allowing orphaned data to appear in the database if a session to browser connection is ever lost.&lt;br /&gt;I will also mention this issue never bubbled up until my app was deployed to a 64-bit machine. In a 32-bit environment it seems I can store all the ActiveRecord objects in a session that I want to.&lt;br /&gt;Learn to purge Oracle&amp;rsquo;s recycle bin&lt;br /&gt;Rails migrations allow the destroying and rebuilding of a project database on a whim.&lt;br /&gt;rake db:migrate VERSION=0; rake db:migrate&lt;br /&gt;A &amp;ldquo;gotcha&amp;rdquo; occurs after running these commands several dozen times or more. Newer versions of Oracle do not actually drop constraints. Instead Oracle saves them to the Oracle recycle bin. Over time they build up and cause otherwise speedy migrations to take longer and longer to run.&lt;br /&gt;The solution is to dump Oracle&amp;rsquo;s recycle bin periodically. Add this to the end (and beginning too if you want) of your ActiveRecord migrations:&lt;br /&gt;execute &amp;lsquo;purge recyclebin&amp;rsquo;&lt;br /&gt;Oracle&amp;rsquo;s Sequence Cache&lt;br /&gt;By default Oracle caches requests for new sequences in blocks of 20. This might become a problem if you need numbers without gaps as you will get 1, 2, 3... and then later 23, 24, 25... To fix it I just added this to the appropriate ActiveRecord::Migration:&lt;br /&gt;execute &amp;lsquo;ALTER SEQUENCE FOO_SEQ NOCACHE&amp;rsquo;&lt;/p&gt;</content>
  <created-at type="datetime">2009-08-30T03:28:38Z</created-at>
  <discuss-url>http://railsmagazine.com/forums/2/topics/73</discuss-url>
  <id type="integer">44</id>
  <issue-id type="integer">4</issue-id>
  <number type="integer">8</number>
  <title>Oracle Tips and Tricks</title>
  <updated-at type="datetime">2009-12-24T20:53:26Z</updated-at>
</article>
