Using default columns in an oracle table, the oracle connection
adapter in ActiveRecord includes single quotes around the default
value in a new object based on that table. Actually it will include
whatever string was used to define the default.
For example; if I have a table that is defined like this:
create table parts.widgets (
id NUMBER,
name varchar2(150) default ''super widget'')
tablespace parts;
and a Widget is defined simply like this:
class Widget < ActiveRecord::Base
end
I can create a new Widget using the console:
Loading development environment.>> w = Widget.new
=> #<Widget:0xb667a640
@attributes={"name"=>"''super widget''"},
@new_record=true>>>
Notice that the default value "super widget" has single quotes around
it. This is not what I want. The single quotes are only used to define
the default in oracle and should not be a part of the default itself.
Edge rails does the same thing.
I have fixed this by changing the type_cast method in oci_adapter.rb
in ActiveRecord by adding this to the "when :string" case:
when :string then
#check if the variable is surrounded by a quote and get rid of it
if value.length >= 3 && value[0,1] == "''"
&& value[value.length - 1, value.length] ==
"''"
value[1, value.length - 2]
else
value
end
I hope the code formatting doesn''t get messed up via email.
I have tested this in both the 13.1 and in Edge Rails and it works as
long as the default was defined with quotes around it and is followed
directly by a comma or a right parenthesis to terminate the default
value definition.
If I don''t get feedback suggesting otherwise I will submit a patch as
soon as I can setup a proper test environment for oracle.
Thank you - Guy