[Ruby] AR migration problems? was: refactoring/drying help

Aaron Patterson aaron at tenderlovemaking.com
Fri Oct 12 13:25:19 PDT 2007


On Fri, Oct 12, 2007 at 01:05:08PM -0700, Jack Danger Canty wrote:
> >
> >  If on migration 39 you had a model MyThingy and by
> > 189 it is now called something Thingy, you only need to go back to 39
> > and add:
> >
> >         class MyThingy < ActiveRecord::Base; end
> >
> > at the top of the file.
> 
> 
> If the model hasn't changed much then it's not to tough to make that change.
>  But what about some really complicated stuff?  For example, I once had a
> migration that looked something like this
> 
> def self.up
>   orphaned_listings = Account.find(:all, :conditions => 'logged_in_at is not
> null').sites.map {|site| site.listings.find(:all, :conditions => 'owner_id
> is null') }
> end
> 
> About 50 migrations later I'd changed the conditions for logged_in_at, the
> sites association had been renamed and listings was now one level removed
> from sites (there was an additional join model).  Urgh.

As long as you make sure to define your relationships in the migration
as well, you should be fine.  For example:

class SomeMigration < ActiveRecord::Migration
  class Account < ActiveRecord::Base
    has_many :listings
  end
  class Listing < ActiveRecord::Base; end
  
  def self.up
    ...
  end

  ...
end

I typically make sure to define the model and any relationships I need
during that migration.

-- 
Aaron Patterson
http://tenderlovemaking.com/


More information about the Ruby mailing list