Hi! In Rails 3.1 there is only one method for migrations called ''change''. I want to run some code in a migration only when migrating up, not down. Is that possible? The issue I have is that this code is inside a migration file from Active Admin: # Create a default user AdminUser.create!(:email => ''admin-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'', :password => ''password'', :password_confirmation => ''password'') So, when I try to migrate down this will fail because there is already a user present. Any ideas? Regards Linus -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/9qqE5Z4agXYJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Peter Vandenabeele
2012-Jan-02 22:52 UTC
Re: Check whether you migrate up or down in Rails 3.1?
On Mon, Jan 2, 2012 at 10:49 PM, Linus Pettersson < linus.pettersson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > > In Rails 3.1 there is only one method for migrations called ''change''. >Not exaclty. up and down still exist (but now instance methods instead of class methods). Google ("rails guide migrations").> > I want to run some code in a migration only when migrating up, not down. > Is that possible? >Yes.> > The issue I have is that this code is inside a migration file from Active > Admin: > > # Create a default user > AdminUser.create!(:email => ''admin-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'', :password => ''password'', :password_confirmation > => ''password'') > > So, when I try to migrate down this will fail because there is already a > user present. >And what about: def up AdminUser.create!(:email => ''admin-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org '', :password => ''password'', :password_confirmation => ''password'') end def down u = AdminUser.find_by_email(''admin-hcDgGtZH8xNBDgjK7y7TUQ@public.gmane.org'') u.destroy end> Any ideas? >If the down migration is really impossible to do (which can happen; e.g because you miss data to reset everything to the original state) than you can raise ActiveRecord::IrreversibleMigration exception in the down migration. HTH, Peter -- Peter Vandenabeele http://twitter.com/peter_v http://rails.vandenabeele.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Linus Pettersson
2012-Jan-02 23:15 UTC
Re: Check whether you migrate up or down in Rails 3.1?
I see. Thank you very much Peter! Regards Linus -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/H8RtP69UakQJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.