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

  • SEARCH
  • Home
  • 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 8299373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:07:46+00:00 2026-06-08T16:07:46+00:00

I have the following Rails 3 migration which adds a column game_type to a

  • 0

I have the following Rails 3 migration which adds a column game_type to a table called games. It’s supposed to update the value of the game_type depending on whether live is true, but this value never gets saved.

class AddGameTypeToGames < ActiveRecord::Migration

  class Game < ActiveRecord::Base
  end

  def up
    say "Adding game_type column to Games table"
    add_column :games, :game_type, :string, :null => false, :default => 'Demo'

    say_with_time "Migrating live value into game_type column" do
      rows_affected = 0
      Game.all.each do |game|
        if game.live
          game.update_attributes!(:game_type => 'Live')
          rows_affected += 1
        end
      end
      rows_affected
    end
  end
end

I eventually got this to work by changing the line game.update_attributes!(:game_type => 'Live') to Game.connection.execute("UPDATE games SET game_type='Live' where id = #{game.id}").

I’m wondering why update_attributes! wouldn’t work? I have other migrations where this works fine. Including the model is supposed to stop validations getting in the way of the migration. I tried setting attr_accessible :game_type on the Game model in the migration, but this didn’t work.

  • 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-06-08T16:07:48+00:00Added an answer on June 8, 2026 at 4:07 pm

    you have to add this line before running the loop on Game model

    Game.reset_column_information
    Game.all.each do |game|
      ....
      ....
    

    Sometimes you’ll want to add a column in a migration and populate it immediately after. In that case, you’ll need to make a call to Base#reset_column_information in order to ensure that the model has the latest column data from after the new column was added.

    There’s an example of this in the API docs for ActiveRecord::Migration. Look for “Using a model after changing its table”

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

Sidebar

Related Questions

I have a table called as language which has a column called as lang_code.
I have the following Rails migration which works perfectly (irrelevant pieces removed): create_table :comments
I have a rails app that requires multi-table inheritance for which I am using
I had a model customer, which I have generated using the following rails g
I have a sequence of migrations in a rails app which includes the following
I have the following rails migration: create_table :articles do |t| t.integer :user_id, :allow_null =>
I have the following Rails model: class CreateFoo < ActiveRecord::Migration def self.up create_table :foo
I have the following rails code in a loop that has a link for
I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
I am using Rails 3.2. I have following models: Blog Comment User class Blog

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.