Models, migrations and data changes


Hi all!

I have one question how You would approach to following issue:

I have live application, and recently I had to change database structure
and remove some models. Now I need to find easy way to do changes in DB
and make data transformation. Of course I’m doing it with migrations.
Quite successfully. With one exception.

Somewhere in migration I wrote following code:

Owner.find(:all).each {|o|
r = Route.find(o[:route_id])
puts “#{o.route_id} not found in routes table”
[some operations on o and r]


Trouble is that no operation after begin/rescue is run - every iteration
through Owners raises ‘undefined method find’. Most strange is Route
class (I have AR model route!):
rake aborted!
undefined method `find’ for

I know about Safely using
models - in my case this is not useful, since I need access to real
modelt properties to make data transition. Using dumb model stub would
give me only access to methods inherited from ActiveRecord::Base, and
this is not enough for me.

Well, I have started digging and at the end I’ve put in migration
following code:

def self.up
puts ActiveRecord::Base::Route.superclass
puts ActiveRecord::Base::Owner.superclass
puts “Surprised?”

And ran migration. Results were:


Well I’m surprised. Is this some special case with Route symbol? Like
other issues I found during my short way with RoR:

Is there some way to access ‘real’ AR::Base::Route object?

As a quick solution I have moved all data transformation to separate
script run by ./script/runner and I break migrations flow raising
exception to give chance run script in right moment. It is ugly hack,
but it works…

If this is issue with some names clash (like with send action or
sessions table) can somebody propose general solution? I don’t know Ruby
and Rails enough to thought about feasible solution…

Witold R.