Return to Snippet

Revision: 1101
at September 9, 2006 09:52 by andyh


Initial Code
def multiple_record_action
  if params[:id]
    @record = CustomRecord.find(params[:id])
    @dependent = @record.dependent
  else
    @record = CustomRecord.new
    @dependent = Dependent.new
    @dependent.record = @record
  end
  if request.post?
    @record.attributes = params[:record]
    @dependent.attributes = params[:dependent]
    begin
      @record.transaction(@record, @dependent) do
        @record.save
        @record.reload unless @record.id
        @dependent.save
        raise ActiveRecord::RecordInvalid unless @record.valid? && @dependent.valid?
        redirect_to :action=>'some_other_action'
      end
    rescue ActiveRecord::RecordInvalid
    end
  end
end

Initial URL


Initial Description


Initial Title
Multiple Record Save Action Pattern

Initial Tags
rails, ruby

Initial Language
Ruby