We recently switched from Mysql to MongoDB, and it’s been a real learning experience. We’re using Mongoid as our object-document mapper, and we’re finding quite a few gotchas from assumptions we made dealing with Mysql. Here’s an interesting one.

Say you have a controller action like this:


    def add_part
      @car = Car.find(params[:car_id])
      @part = @car.parts.create!(params[:part])
      respond_with @part
    end

@part will save, provided it passes validation. @car may not, though. Say you updated the validation rules on the model, but some of the Car documents still have invalid values on them — @car will not save if those fields are invalid. Worse, it will fail silently, so @part will exist and be valid in your controller code, and @car will be accessible through it. Only if you call @car.reload will all that controller work disappear.

So, if your associations (relational or embedded) are disappearing mysteriously, popping open the rails console and checking for this situation with your models and making sure to call parent.reload is a good place to start.