Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6685967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:07:05+00:00 2026-05-26T05:07:05+00:00

I really like the Rails 3 style migrations, i.e. one change method being smart

  • 0

I really like the Rails 3 style migrations, i.e. one change method being smart enough to recognize if the migrations is being installed or rolled back, so I don’t have to write up and down methods mirroring each other. But I have situation that I need to skip some code when the migration is rolled back (updating counter_cache columns that I’m adding).

I looked at http://guides.rubyonrails.org/migrations.html but the examples at the end of section 5 suffer from the same problem:

class AddFuzzToProduct < ActiveRecord::Migration
  class Product < ActiveRecord::Base
  end
  def change
    add_column :products, :fuzz, :string
    Product.reset_column_information
    Product.all.each { |f| f.update_attributes! :fuzz => 'fuzzy' }
  end
end

When this migration is rolled back, the update of fuzz field is unnecessary. Is there a way to prevent it?

I tried looking into Product.column_names but since Rails is smart enough to perform migration in reverse direction, the update is executed before the column is removed. Also, when change method is defined, any up or down methods seem to be ignored. Any other ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:07:06+00:00Added an answer on May 26, 2026 at 5:07 am

    In this case I think you’ll have to use up and down methods as usual. Don’t worry, despite the addition of change in Rails 3 those methods aren’t, as far as I know, bound for the chopping block. Continue using them where necessary.

    Edit: Here’s an option: Override migrate.

    class AddFuzzToProduct < ActiveRecord::Migration
      class Product < ActiveRecord::Base
      end
    
      def change
        add_column :products, :fuzz, :string
      end
    
      def migrate(direction)
        super # Let Rails do its thing as usual...
    
        if direction == :up # ...but then do something extra if we're going 'up.'
          Product.reset_column_information
          Product.all.each { |f| f.update_attributes! :fuzz => 'fuzzy' }
        end
      end
    end
    

    Thoughts?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I couldn't really find this in Rails documentation but it seems like 'mattr_accessor' is
I really like how in Rails 3 you can ask a scope for its
I really like git. At least, I like the idea of git. Being able
I'm very new to rails but I really like how easy it is to
I've read this article, but it's for Rails 1.x. I'd really like to create
I really like php, but I like how rails organizes it's files. I'm wondering
I really like Sunspot in Rails, but it adds way too much to my
I am building an application in Rails 3.1 and I really like the whole
I really like the Rails authorization gem CanCan . However, I find myself having
I really like DevX components, but they are pretty expensive, maybe anyone knows free

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.