Search code examples
ruby-on-railscallback

Using ActiveModel Dirty appropriately in a controller action


A rails controller action has the usual boilerplate structure

  def update
    respond_to do |format|
      if @individual.update(individual_params)
        format.html { redirect_to @individual, notice: "Individual was successfully updated." }
       # if @individual.changed?  
        render_pdf
      # end
      else
        format.html { render :edit, status: :unprocessable_entity }
      end
    end
  end

Howeve, the goal is to - where the record has been updated (aside from the updated_at attribute) - activate a method that will generate a PDF.

Consulting the documentation for dirty, I fail to generate a true response to the changed? method. I suspect this is because of the timing of the method being invoked (if @individual.update implies the record is already saved and thus the dirtied bits cleaned up).

How should the above be written to achieve the goal?

alternative
a callback after_update_commit might be an option, however, the rails guide is not explicit as to whether the action is launched when no attributes are updated.


Solution

  • if @individual.previous_changes.present?