Migration fails need to reload attributes from model?

hello,

I have a problem with a migration sequence. Running rake db:migrate from
the start (empty db) fails at migration 20. The problem it appears, is
that the new attributes are not reloaded. I have tried adding "load
‘state.rb’ to the migration but this also fails.

If i run the migration again from the failed point it succeeds.

i would like to be able to run the complete set of migrations to create
the db.

Thanks in advance for any help.


Error:
== RePopulateStates: migrating

rake aborted!
undefined method page_name=' for #<State:0x355f728> /Users/adam/Projects/boomkat/acquire.boomkat.com/trunk/vendor/rails/activerecord/lib/active_record/base.rb:1934:inmethod_missing’
/Users/adam/Projects/boomkat/acquire.boomkat.com/trunk/vendor/rails/activerecord/lib/active_record/base.rb:1749:in
send' /Users/adam/Projects/boomkat/acquire.boomkat.com/trunk/vendor/rails/activerecord/lib/active_record/base.rb:1749:inattributes=’


migration 19) The first migration adds 4 columns.
eg

add_column :states, :page_name, :string, :default => ''
add_column :states, :body_class, :string, :default => ''
add_column :states, :path, :string, :default => ''

migration 20) The second clears out the model and repopulates:
eg

State.destroy_all
State.create!(:name => ‘received’, :page_name => ‘Inbox’,
:body_class => ‘inbox’, :path=> ‘releases_path’,
:empty_text => ‘No releases have have imported at this time.’)

State.create!(:name => 'accepted',  :page_name => 'Edit and

Publish’, :body_class => ‘edit_and_publish’, :path=>
‘publish_releases_path’, :empty_text => ‘No releases have have
completed publication at this time.’)

On May 31, 2007, at 9:15 AM, Adam M.ox wrote:

================================================
`attributes=’
migration 20) The second clears out the model and repopulates:
completed publication at this time.’)
Whenever you use a Model class in a migration, you should define it
within the migration class. This avoids problems related to the
state of the model.

For example:

class AddProductSource < ActiveRecord::Migration
class Product < ActiveRecord::Base
end

def self.up
add_column :products, :source, :string
Product.reset_column_information
Product.update_all(‘source = “something”’)
add_index :products, [ :source, :vendor, :code ], :name =>
‘products_source_index’
end

def self.down
remove_index :products, :name => ‘products_source_index’
remove_column :products, :source
end
end

You also need to call the .reset_column_information to force
ActiveRecord to notice the changes that the migration has made to the
database.

Try putting a “class State < ActiveRecord::Base; end” inside your
migration 20 and see if that clears things up.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Thanks bob!!

Im not sure how i missed the reset_column_information method. Ill blame
the hot chilli thai meal and beer i had at lunch.

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