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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:08:18+00:00 2026-05-27T18:08:18+00:00

Can someone give me a short introduction to doing DB migrations in Rails using

  • 0

Can someone give me a short introduction to doing DB migrations in Rails using Mongoid? I’m particularly interested in lazy per document migrations. By this, I mean that whenever you read a document from the database, you migrate it to its latest version and save it again.

Has anyone done this sort of thing before? I’ve come across mongoid_rails_migrations, but it doesn’t provide any sort of documentation, and although it looks like it does this, I’m not really sure how to use it.

I should point out I’m only conceptually familiar with ActiveRecord migrations.

  • 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-27T18:08:19+00:00Added an answer on May 27, 2026 at 6:08 pm

    If you want to do the entire migration at once, then mongoid_rails_migrations will do what you need. There isn’t really much to document, it duplicates the functionality of the standard ActiveRecord migration. You write your migrations, and then you use rake db:migrate to apply them and it handles figuring out which ones have and haven’t been ran. I can answer further questions if there is something specific you want to know about it.

    For lazy migrations, the easiest solution is to use the after_initialize callback. Check if a field matches the old data scheme, and if it does you modify it the object and update it, so for example:

    class Person
        include Mongoid::Document
    
        after_initialize :migrate_data
    
        field :name, :type => String
    
        def migrate_data
            if !self[:first_name].blank? or !self[:last_name].blank?
                self.set(:name, "#{self[:first_name]} #{self[:last_name]}".strip)
                self.remove_attribute(:first_name)
                self.remove_attribute(:last_name)
            end
        end
    end
    

    The tradeoffs to keep in mind with the specific approach I gave above:

    If you run a request that returns a lot of records, such as Person.all.each {|p| puts p.name} and 100 people have the old format, it will immediately run 100 set queries. You could also call self.name = "#{self.first_name} #{self.last_name}".strip instead, but that means your data will only be migrated if the record is saved.

    General issues you might have is that any mass queries such as Person.where(:name => /Foo/).count will fail until all of the data is migrated. Also if you do Person.only(:name).first the migration would fail because you forgot to include the first_name and last_name fields.

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

Sidebar

Related Questions

Can someone give me a short and plausible explanation for why the compiler adds
I'm hoping someone has run into this sort of problem before, and can give
Can someone give an example of a good time to actually use unsafe and
Can someone give some hints of how to delete the last n lines from
Can someone give me some example code that creates a surface with a transparent
Can someone give me an example of how I would delete a row in
Can someone give me some info/background info on how I might go about writing
Can someone give me a real-world scenario of a method/function with a string argument
Can someone give an example for finding greatest common divisor algorithm for more than
can someone give me a hint on how a histogram's pseudo code would look

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.