Rails 2.0: XML data type and DB2
Posted on January 2, 2008
Filed Under Rails, Ruby |
[2008/04/30] Update: The ibm_db gem has been updated to support the new Rails 2.0 style migration. Now you can just use t.xml and it will work. Look here for more info. Thanks to Mario Briggs for pointing me to the update.
—
In my new job we work with XML data natively stored on DB2. I have done some test with Rails and DB2 a few weeks ago and it’s pretty interesting, specially the easiness that Rails deals with XML.
We decided to test some new features of DB2 9.5 and I decided to test them with Rails 2.0.
So I created a new Rails app, configured the database.yml to use the idm_db gem and fired:
$ rake db:create
The output is nice but, the database isn’t there! I suppose it’s something with the gem that needs to be updated. But really don’t know about it. It just doesn’t work. But this will not hold us down. I will just create the database by hand and keep going with my tests.
After that, I created a new model XmlDocument, which has a column named data, of type XML. According to the new migrations syntax, my model’s migration would look something similar with this:
class CreateXmlDocuments < ActiveRecord::Migration
def self.up
create_table :xml_documents do |t|
t.xml :data
t.timestamps
end
end
def self.down
drop_table :xml_documents
end
end
Right? Ok, I fired up a terminal and lauched:
$ rake db:migrate
The output? Here it is:
== 3 CreateXmlDocuments: migrating ============================================
– create_table(:xml_documents)
rake aborted!
undefined method `xml’ for #
Yes, a big nice exception! The new migration syntax doesn’t allow you to do t.xml! But please don’t ask me why! :p
The solution? Well, although it’s weird - imo - , it’s also easy. You can mix the new and the old syntax in the same file. So our model’s migration will now look like this:
class CreateXmlDocuments < ActiveRecord::Migration
def self.up
create_table :xml_documents do |t|
t.column :data, :xml
t.timestamps
end
end
def self.down
drop_table :xml_documents
end
end
And that’s it! We’re ready to go! I didn’t find anything else different so far, besides what I described here. Hope this helps!
Comments
7 Responses to “Rails 2.0: XML data type and DB2”
Leave a Reply


Maybe worth waiting until the IBM team give us an ibm_db which supports Rails 2.0 : which they are currently working on as top priority.
The “t.xml” is new Rails 2.0 syntax.
If you use the older syntax and specify the data type as xml everything will work fine.
Yes I heard they are working on it.
And about the older syntax, that’s exactly what I’ve don in the end of the article!
Tks
Excellent
I was looking for this exact thing.
And yes IBM_DB has to be changed. You have to disable support for schemas and I had to hardcode my username in the sources because it doesn’t accept a username ‘null’ :X That’s my linux username, i’m lame
Anyway great fix. I was doubting this would work, but it did
Will add your blog to my feeds
Have a nice week
Hey man!
I’m glad it helped you. Since you’re working with IBM DB2, you might like to know that there is a bug with IBM_DB loading XML data from fixture files. Did u hit it yet? If you want, drop a message, we can discuss it.
[…] Rails 2.0: XML data type and DB2 : leonardoborges.com (tags: rails db2 fix xml purexml ibm_db) […]
The ibm_db gem has been updated to support the “t.xml” syntax - http://rubyforge.org/forum/forum.php?forum_id=24055
>>
you might like to know that there is a bug with IBM_DB loading XML data from fixture files
<<
This was fixed a couple of weeks ago. http://rubyforge.org/forum/forum.php?forum_id=25464
Thanks to Roderick Van Domburg for highlighting how fixtures are handled differently from Model.save