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 8594873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:17:58+00:00 2026-06-12T00:17:58+00:00

The Rails migration guide , suggests you create a faux model inside the migration,

  • 0

The Rails migration guide, suggests you create a faux model inside the migration, if you need to operate on the data from the database, like:

class AddFuzzToProduct < ActiveRecord::Migration
  class Product < ActiveRecord::Base
  end

  def change
    add_column :products, :fuzz, :string
    Product.reset_column_information
    Product.all.each do |product|
      product.update_attributes!(:fuzz => 'fuzzy')
    end
  end
end

The thing is, inside the AddFuzzToProduct class, the name of the Product model will be AddFuzzToProduct::Product. I have the following situation:

class RemoveFirstNameFromStudentProfile < ActiveRecord::Migration

  class StudentProfile < ActiveRecord::Base
    has_one :user,:as => :profile
  end

  class User < ActiveRecord::Base
    belongs_to :profile,:polymorphic => true,:dependent => :destroy
  end

  def up
    StudentProfile.all.each do |student|
       # I need to do some work on the user object as well as on the student object
       user = student.user
       ... # do some stuff on the user object
    end
  end

end

The thing is, inside the each block for the student profile, user is nil. After I activated the logger, I can see that Rails is trying to do the following query:

 RemoveFirstNameFromStudentProfile::User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."profile_id" = 30 AND "users"."profile_type" = 'RemoveFirstNameFromStudentProfile::StudentProfile' LIMIT 1

Of course, this can be fixed by moving the User and StudentProfile up one level, like in:

  class StudentProfile < ActiveRecord::Base
    has_one :user,:as => :profile
  end

  class User < ActiveRecord::Base
    belongs_to :profile,:polymorphic => true,:dependent => :destroy
  end

  class RemoveFirstNameFromStudentProfile < ActiveRecord::Migration
    def up
      StudentProfile.all.each do |student|
        ...
      end
    end
  end

My question is: can moving the definitions of the faux models outside of the declaration of the migration cause any problems for me? Is there something I’m missing here? Why did the guys from the Rails team declare them inside the migration class?

  • 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-12T00:17:59+00:00Added an answer on June 12, 2026 at 12:17 am

    No it will not cause any problem for you as you are not adding any new columns and updating it.

    Rails team has declared it inside the migration because they were adding a new column and then updating it but if Model is outside and it will try to validate that column which is not possible as it was not there, it was just added in migration. Due to that reason they have created local models in migration only for more read this Using Models in your migrations

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

Sidebar

Related Questions

I quite like Rails' database migration management system. It is not 100% perfect, but
I attempted to generate a migration like: rails generate migration RemovefromGenotypes box:integer well:string but
I needed an id field in my rails migration that auto-increments from 0 and
What's the syntax for dropping a database table column through a Rails migration ?
i have a rails migration: >> cat db/migrate/20091126031039_create_cards.rb class CreateCards < ActiveRecord::Migration def self.up
I would like to make a column unique in Ruby on Rails migration script.
I removed several tables in my Rails database by running rails generate migration RemoveObjects
I have a Rails migration that looks like this: puts *** What would you
Is it possible to create (self.up) multiple tables in one rails three migration. If
I saw in rails guides that, mentioning foreign key in model doesn't really create

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.