There are three main options:
- Update everything inside MongoDB to enable excerpt.
- Use the
after_initialize hook to add a default excerpt to existing objects when you pull them out of MongoDB. - Lock your validation logic to only check for excerpts from new objects.
(1) it takes a (possibly big) hit in time when you make a change, but this is just one time, and you don't need to worry about it after that. You pull each page out of MongoDB, do page.excerpt = 'some default excerpt' , and then save it back to MongoDB. If you have many pages, you will want to process them in pieces, say, 100 at a time. If you do this, you can search in excerpts without worrying about what you should do with null s. You can also do this inside MongoDB by sending a JavaScript fragment to 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 look something like this:
after_initialize :add_default_excerpt, :unless => :new_record?
You can move unless self.excerpt to :unless , if you don't mind using lambda:
after_initialize :add_default_excerpt, :unless => ->{ |o| o.new_record? || o.excerpt.present? }
This should be pretty quick and easy to set up, but there are also disadvantages. First of all, in your MongoDB you will have a bunch of null , which you may have to handle specifically during the search. In addition, you will be carrying a bunch of code and logic to process old data, but this baggage will be used less and less over time. In addition, the after_initialize call after_initialize not come for free.
(3) requires that you skip checking the availability of excerpts for non-new pages ( :unless => :new_record? ), Or you will have to find a way to distinguish new objects from old ones, as well as correctly process changes, both new and new and old pages. You can also get people to provide an excerpt when they change the page and leave your check as is; including :default => '' on your field :excerpt , will take care of any nil problems in the views, etc.
If possible, I will go with (1). If the update takes too much time and you want the site to work and work while you are fixing MongoDB, you can add :default => '' when updating, and then remove the :default option, restart and manually fix all the deviations that were received through.