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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:02:46+00:00 2026-05-28T17:02:46+00:00

I’m just diving into Mongodb and MongoID with Rails and I find it awesome.

  • 0

I’m just diving into Mongodb and MongoID with Rails and I find it awesome. One thing the NoSQL helps is when I can add extra fields to my model without any extra effort whenever I want:

class Page
  include Mongoid::Document
  include Mongoid::MultiParameterAttributes
  field :title, :type => String
  field :body, :type => String
  field :excerpt, :type => String #Added later
  field :location, :type => String #Added later
  field :published_at, :type => Time

  validates :title, :presence => true
  validates :body, :presence => true
  validates :excerpt, :presence => true
end

And this works perfectly as it should. But my question is, (sorry if this is trivial) the existing entries are blank and have no defined value for the newly added field. For example, in a sample blog application, after I’ve published two posts, I decide to add an excerpt and a location field to my database (refer code above). Any blog post that is published after the addition of these new fields can be made sure to have a value filled in for the excerpt field. But the posts published prior to the addition of these two new fields have null values (which is understandable why) which I cannot validate. Is there an elegant solution for this?

Thank you.

  • 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-28T17:02:47+00:00Added an answer on May 28, 2026 at 5:02 pm

    There are three basic options:

    1. Update everything inside MongoDB to include the excerpt.
    2. Use an after_initialize hook to add a default excerpt to existing objects when you pull them out of MongoDB.
    3. Kludge your validation logic to only check for the existence of excerpt on new objects.

    (1) requires a (possible large) time hit when you make the change but it is just a one time thing and you don’t have to worry about it after that. You’d pull every Page out of MongoDB, do page.excerpt = 'some default excerpt', and then save it back to MongoDB. If you have a lot of Pages you’ll want to process them in chunks of, say, 100 at a time. If you do this, you’ll be able to search on the excerpt without worrying about what you should do with nulls. You can also do this inside MongoDB by sending a JavaScript fragment into MongoDB:

    connection.eval(%q{
        db.pages.find({}, { _id: true }).forEach(function(p) {
            db.pages.update(
                { _id: p._id },
                { $set: { excerpt: 'some default excerpt' } }
            );
        });
    })
    

    (2) would go something like this:

    after_initialize :add_default_excerpt, :unless => :new_record?
    #...
    private
    def add_default_excerpt
      self.excerpt = 'some default excerpt' unless self.excerpt.present?
    end
    

    You could move the unless self.excerpt up to the :unless if you didn’t mind using a lambda:

    after_initialize :add_default_excerpt, :unless => ->{ |o| o.new_record? || o.excerpt.present? }
    #...
    private
    def add_default_excerpt
      self.excerpt = 'some default excerpt'
    end
    

    This should be pretty quick and easy to set up but there are downsides. First of all, you’d have a bunch of nulls in your MongoDB that you might have to treat specially during searches. Also, you’d be carrying around a bunch of code and logic to deal with old data but this baggage will be used less and less over time. Furthermore, the after_initialize calls do not come for free.

    (3) requires you to skip validating the presence of the excerpt for non-new Pages (:unless => :new_record?) or you’d have to find some way to differentiate new objects from old ones while also properly handling edits of both new and old Pages. You could also force people to supply an excerpt when they change a Page and leave your validation as-is; including a :default => '' on your field :excerpt would take care of any nil issues in views and such.


    I’d go with (1) if possible. If the update would take too long and you wanted the site up and running while you were fixing up MongoDB, you could add a :default => '' while updating and then remove the :default option, restart, and manually patch up any strays that got through.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.