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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:21:51+00:00 2026-05-23T09:21:51+00:00

I have model as follows: class User include Mongoid::Document field :name end After saving

  • 0

I have model as follows:

class User
  include Mongoid::Document
  field :name
end

After saving a few user objects to the database I added some more fields:

class User
  include Mongoid::Document
  include Mongoid::Timestamps::Created
  field :name
  field :birthdate
end

Now, I expect I can use the following snippet:

@user = User.all
@user.each do |u|
   puts u.name
   puts u.birthdate.strftime(#someFormat)
   puts u.created_at.strftime(#someFormat)
end

Unfortunately, because my old user objects don’t have birthdate keys, I receive this error: strftime called on nil class.

Question:

  • How can I handle such cases with Mongoid? In MySQL, when a column is added it gets added to old rows as well. But if I see in MongoDB, it doesn’t add new fields as keys for old data.
  • This problem also exists with the created_at field as old data doesn’t have that field either.

I am looking for good way to solve this, checking for nil conditions each time is not a scalable option because the number of fields will continue increasing.

  • 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-23T09:21:52+00:00Added an answer on May 23, 2026 at 9:21 am

    You could also add a default to your new field, which would give all your old records a fallback value.

    class User
      include Mongoid::Document
      include Mongoid::Timestamps::Created
    
      field :name
      field :birthdate, :type => Date, :default => Date.new(1970,1,1)
    end
    

    You can update your models which are missing created_at values, but just running something simple in your rails console:

    User.all.each{ |u| u.update_attributes(:created_at => Time.now) if u.created_at.nil? }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two model as follows class User < ActiveRecord::Base validates_associated :account end class
I have a model as follows: class User < ActiveRecord::Base attr_accessible :name, :email, :twitter,
I have a Model as follows: class TankJournal(models.Model): user = models.ForeignKey(User) tank = models.ForeignKey(TankProfile)
I have a model as follows class UserPrivacy(models.Model): user = models.ForeignKey(User) profile = models.SmallIntegerField(default=1,
I have a model that is set up as follows: class Log(models.Model): name =
I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car <
I have models as follows: class Book(models.Model): title = models.CharField(max_length=400) def transactions_all_time(self): latest_transactions =
1] I have the following model: class UserReportedData(db.Model): #country selected by the user, this
I have a model as follows: class EntityTag < ActiveRecord::Base attr_protected :user_id, :post_id, :entity_id
I have two models in Django like follows(in pseudo code) class Medicine(db.Model): field_1 =

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.