Wednesday, August 05, 2009

Differences JRuby JDBC-Adapter for mysql and Ruby MysqlAdapter in method: pk_and_sequence_for


This will result in a bad schema.rb when using non default primary-keys and thus test databases are not created correct.

JRuby

>> connection = ActiveRecord::Base.connection
=> #<ActiveRecord::ConnectionAdapters::JdbcAdapter:0x321bc965>
>> connection.respond_to?(:pk_and_sequence_for)
=> false
>> connection.respond_to?(:primary_key)
=> false


Ruby

>> connection = ActiveRecord::Base.connection
=> #<ActiveRecord::ConnectionAdapters::MysqlAdapter:0x19ad73c @co...
>> connection.respond_to?(:pk_and_sequence_for)
=> true
>> connection.respond_to?(:primary_key)
=> false

Friday, July 24, 2009

Running Rails 2.3.3 on JRuby 1.3.1 with MS-SQL 2008


This document is derived from codersifu - Sudirman

We need to run Rails 2.3.3 on JRuby 1.3.1 (Java 6) in combination with MS-SQL Server 2008.
There is no activerecord-jdbc-adapter for MS SQL Server so you need to do something else.

This should work on Mac OS-X Leopard and Debian Lenny.

jruby -S gem install activerecord-jdbc-adapter

Download MS JDBC driver version 2.0 from Microsoft MSDN

After your download complete, extract sqljdbc4.jar into your jruby\lib directory.

You need to modify your database.yml. Here is the example:


development:
adapter: jdbc
username: sa
password:
driver: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://database-server;databaseName=trogdor_development

Monday, April 13, 2009

Be careful with callbacks and log messages in Rails



A lot of times I see a very annoying 'feature' in rails projects.

People using log messages in some callbacks of ActiveRecord to display some values.

before_save :add_generated_order_number

def add_generated_order_number
self.order_number = SequenceNumber.next_order_number
log.debug "Added #{order_number}"
end

This is potentially dangerous, since log has a very nice feature. It returns a value depending on the log level. If log level is on DEBUG (typically development and test mode setting) it will return TRUE, if log level is on INFO (typically production mode setting) it will return FALSE, breaking the chain of callbacks, thus the record is NOT being saved. Make sure you're testing your code against production mode. Put the loglevel in your test environment setting to the same value as your production setting.

See add function return values:
http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/classes/Logger.html#M000901