Search code examples
ruby-on-railsbackbone.js

Backbone/Rails Identify What New Items Are Saved?


Is there any way in Rails to identify what new attributes are saved to a Rails model?

For example let's say the first time I update an Appuser I save them with gender: "1", and the next time I save them with age: "27". Is there any good way to tell what new parameters have come back and are being saved? (Note - this is coming back from a Backbone save call, so for the second call you would see both age and gender).

Basically for each time I want to know that, oh this time you're adding an age and this time you're adding a gender. Without having to check every attribute.

    @appuser = Appuser.find(params[:id])
    respond_to do |format|
      if @appuser.update(appuser_params)

      end
        format.json { head :no_content }
      else
        format.json { render json: @appuser.errors, status: :unprocessable_entity }
      end

Solution

  • This has to be handled on the Backbone.js side. It's too late to avoid checking each attribute by the time it gets to Rails.

    To do this, Rails should be receiving a PATCH outlining what has changed. The backbone docs outline how to make a PATCH request, as part of the save docs.