views:

142

answers:

1

I have a scenario where I am trying to do a proof of concept using Rails on a legacy db2 database to get buy off that it is a viable option platform for us to use for current and future development.

All of our tables have a primary key which is defined as char for bit data, which is binary. Is there any way I can define a mapper to convert that data to/from a string?

I'm a ruby/rails newb so this may be an easy question to answer, I just haven't been able to find it.

+1  A: 

Override the setter and getter for the attribute.

# Assumption:
# 1. UUID is a BINARY column in your DB2 table. 
# 2. UUID column should be handled as a string
#
class User < ActiveRecord::Base

  def UUID
     convert_to_str(attributes['UUID'])
  end  

  def UUID=(str)
     attributes['UUID'] = convert_to_bin(str)
  end  

  def convert_to_str bin
     # custom conversion 
  end

  def convert_to_bin str
     # custom conversion 
  end
end
KandadaBoggu
Your assumptions are correct. I'll give this a shot. Thanks for your feedback.
jridley
Shouldn't UUID=(str) return the unmodified string instead of the converted? Means: Insert a second line only reading "str".
hurikhan77