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:in `method_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:in `attributes='' --- 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.'') -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2007-May-31 14:12 UTC
Re: Migration fails need to reload attributes from model?
On May 31, 2007, at 9:15 AM, Adam Maddox wrote:> > 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:in > `method_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:in > `attributes='' > > --- > 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.'')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 Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
Adam Maddox
2007-May-31 15:55 UTC
Re: Migration fails need to reload attributes from model?
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---