Strange Migration Behaviour

Hi! I’m getting this strange migration error for this migration:

class CreateCustomers < ActiveRecord::Migration
def self.up
Customer.create :name => ‘TEST’, :zone_id => 0
end

def self.down
Customer.delete_all
end
end

Produces:

== CreateCustomers: migrating

– create({:name=>“TEST”, :zone_id=>0})
rake aborted!
undefined method `create’ for
#ActiveRecord::ConnectionAdapters::MysqlAdapter:0x46c2204

I “fixed” it by defining the customer model inside the migration. Why is
happening this? (the same kind of migrations are working for all my
models except “Customer”)

class CreateCustomers < ActiveRecord::Migration
class Customer < ActiveRecord::Base
end

def self.up
Customer.create :name => ‘TEST’, :zone_id => 0
end

def self.down
Customer.delete_all
end
end

Perhaps a little “obvious”, but
“eval(File.read(‘app/models/customer.rb’))” also works fine.

class CreateCustomers < ActiveRecord::Migration

Improvised fix. Why?

eval(File.read(‘app/models/customer.rb’))

def self.up
Customer.create :name => ‘TEST’, :zone_id => 0
end

def self.down
Customer.delete_all
end
end

I think thisis related:
http://wiki.rubyonrails.org/rails/pages/TipsAndTricks, Adding Data to
Migrations

I still don’t know why! :frowning:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs