Personal blog of Jack Casey.
RSS icon Email icon Home icon
  • Rails Gotcha

    Posted on May 16th, 2011 beetlefeet No comments
    So this was a bit of a baby puncher this arvo…

    If you add a column in a rails migration, rails will NOT correctly save to column in the same migration. You can access it and assign to it, but it will not be saved to the database ever.)

    For example if you have something like:

    class UpdateSubscriptions < ActiveRecord::Migration
      def up
        add_column :subscriptions, :user_id, :integer
        Subscription.all.each do |s|
          s.user = User.find_by_subscription_key( s.key )
          s.save
      end
    end

    Rails happily ignores your change and silently only updates the "updated_at" column (if you have timestamp columns).

    So split this into 2 migrations! Or perhaps some amount of "reload!" type action might work as well..

    No TweetBacks yet. (Be the first to Tweet this post)

    Comments are closed.