Tuesday, January 5, 2010

Fix for undefined method `define_a_column' for class `OCI8::Cursor' (NameError) in OS X 10.6 (Snow Leopard)

If you're using Ruby with ruby-oci8 in OS X 10.6 (Snow Leopard), you may get the following error:
$ ./script/server
=> Booting WEBrick...
./script/../config/../vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb:536: undefined method `define_a_column' for class `OCI8::Cursor' (NameError)
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require'
from ./script/../config/../vendor/rails/activerecord/lib/active_record.rb:81
from ./script/../config/../vendor/rails/activerecord/lib/active_record.rb:80:in `each'
from ./script/../config/../vendor/rails/activerecord/lib/active_record.rb:80
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:162:in `require_frameworks'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:162:in `each'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:162:in `require_frameworks'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:83:in `process'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:43:in `send'
from ./script/../config/../vendor/rails/railties/lib/initializer.rb:43:in `run'
from ./script/../config/../config/environment.rb:11
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require'
from ./script/../config/../vendor/rails/railties/lib/commands/servers/webrick.rb:52
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:342:in `new_constants_in'
from ./script/../config/../vendor/rails/activesupport/lib/active_support/dependencies.rb:495:in `require'
from ./script/../config/../vendor/rails/railties/lib/commands/server.rb:39
from ./script/server:3:in `require'
from ./script/server:3

First off, read Raimonds Simanovskis's great post on how to setup Ruby and Oracle instant client on Snow Leopard, if you haven't already. Note that the environment variables are different for different operating systems (see Oracle documentation). You may get the same or similar error by not having an environment variable missing/misspelled/setup incorrectly.

If you get the error I mentioned above, you might have a problem with version 2.0.0 and higher of ruby-oci8 as noted by Brendan Boesen. Assuming you have everything else working, just uninstall ruby-oci8 and install the older version, compiling it for 64-bit Intel architecture via:

gem uninstall ruby-oci8
env DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH ARCHFLAGS="-arch x86_64" gem install ruby-oci8 --version "< 2.0.0"
After doing that, I tried ./script/server again, and it worked fine.

Update: October 27, 2010: With Rails 3 you can use Ruby-oci8 2.0.4, and Oracle Enhanced Adapter 1.3.1. See this post.

No comments: