为什么after_commit过滤器会调用该函数两次?

时间:2013-10-17 15:47:49

标签: ruby-on-rails ruby filter

我刚刚意识到当我使用after_commit过滤器时,它似乎正在执行该函数两次(原因我不知道)

控制器:

def new
    @upload_files = UploadFiles.new
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @upload_files }
    end
  end

  # GET /uploads/1/edit
  def edit
    @uploadFiles = Upload.find(params[:id])
  end

  # POST /uploads
  # POST /uploads.json
  def create
    @upload_files = UploadFiles.create(params[:upload_files])
    respond_to do |format|
      if @upload_files.save
        redirect_to @upload_files
      else
        format.html { render action: "new" }
        format.json { render json: @upload_files.errors, status: :unprocessable_entity }
      end
    end
  end

模板:

<%= csrf_meta_tags %>
<%= form_for :upload_files, :url => upload_files_path, :html => { :multipart => true } do |f| %>
<h4>Upload Inventory</h4>
<div><%= f.file_field :inventory %></div>
<h4>Upload Material List</h4>
<div><%= f.file_field :material_list %></div>
&nbsp;
<div align="center">
    <%= f.submit "Upload" %>
</div>
<% end %>

class UploadFiles < ActiveRecord::Base
  after_save :process_files

def process_files
    @init_process=Time.now
    out_file = File.new("times.txt", "w")
    out_file.puts("Init Time")
    out_file.puts(Time.now)
    logger.info "Processing the request..."
    logger.info Time.now
    logger.info "Processing Files..."
    logger.info Time.now
.
.
.
end
end

日志:

Started POST "/upload_files" for 127.0.0.1 at 2013-10-17 10:20:06 -0430
Processing by UploadFilesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"NpCyDUNq8uPwJMj2DofP4rHEZWYkfsIu68Wg+XqebNk=", "upload_files"=>{"inventory"=>#<ActionDispatch::Http::UploadedFile:0x4df7648 @original_filename="INV Onhand -753233-2013090621595800.xlsx", @content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", @headers="Content-Disposition: form-data; name=\"upload_files[inventory]\"; filename=\"INV Onhand -753233-2013090621595800.xlsx\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n", @tempfile=#<File:C:/Users/V80042~1/AppData/Local/Temp/RackMultipart20131017-7356-1i1m655>>, "material_list"=>#<ActionDispatch::Http::UploadedFile:0x4df7408 @original_filename="Formato SCL Movistar1.xlsx", @content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", @headers="Content-Disposition: form-data; name=\"upload_files[material_list]\"; filename=\"Formato SCL Movistar1.xlsx\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n", @tempfile=#<File:C:/Users/V80042~1/AppData/Local/Temp/RackMultipart20131017-7356-fqdioq>>}, "commit"=>"Upload"}
  [1m[35mUser Load (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
  [1m[36m (0.0ms)[0m  [1mbegin transaction[0m
Binary data inserted for `string` type on column `inventory_content_type`
Binary data inserted for `string` type on column `material_list_content_type`
  [1m[35mSQL (2.0ms)[0m  INSERT INTO "upload_files" ("created_at", "inventory_content_type", "inventory_file_name", "inventory_file_size", "inventory_updated_at", "material_list_content_type", "material_list_file_name", "material_list_file_size", "material_list_updated_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Thu, 17 Oct 2013 14:50:06 UTC +00:00], ["inventory_content_type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"], ["inventory_file_name", "INV_Onhand_-753233-2013090621595800.xlsx"], ["inventory_file_size", 6776337], ["inventory_updated_at", Thu, 17 Oct 2013 14:50:06 UTC +00:00], ["material_list_content_type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"], ["material_list_file_name", "Formato_SCL_Movistar1.xlsx"], ["material_list_file_size", 42608], ["material_list_updated_at", Thu, 17 Oct 2013 14:50:06 UTC +00:00], ["updated_at", Thu, 17 Oct 2013 14:50:06 UTC +00:00]]
  [1m[36m (4.0ms)[0m  [1mcommit transaction[0m
Processing the request...
2013-10-17 10:20:07 -0430
Processing Files...
2013-10-17 10:20:07 -0430
Inventory Opened...
2013-10-17 10:21:47 -0430
Material List Opened...
2013-10-17 10:21:48 -0430
Default Sheets were set...
Output file created...
2013-10-17 10:21:48 -0430
Code and Name read...
2013-10-17 10:21:48 -0430
Beginning Inventory reading...
2013-10-17 10:21:48 -0430
Inventory completely read...
2013-10-17 10:57:54 -0430
Total Time reading and Parsing Inventory...
2013-10-17 10:57:54 -0430
Output file created...
2013-10-17 10:58:00 -0430
Process Finished...
2013-10-17 10:58:00 -0430
  [1m[35m (1095.0ms)[0m  begin transaction
  [1m[36m (19.0ms)[0m  [1mcommit transaction[0m
Processing the request...
2013-10-17 10:58:03 -0430
Processing Files...
2013-10-17 10:58:03 -0430
failed to allocate memory
Redirected to http://localhost:3000/upload_files/157
Completed 406 Not Acceptable in 2293951ms (ActiveRecord: 1124.0ms)

该文件显然太大而且抛出无法分配内存但是如果它没有我很确定它会再次执行它...

为什么要再次执行呢?

我尝试使用after_create和after_save,但它抛出一个异常,说.xlsx文件不存在。

1 个答案:

答案 0 :(得分:1)

假设我理解了你的意思,你在控制器的create操作中进行了多次保存,因为UploadFiles.create@upload_files.save都会触发数据库操作。

也许你的意思是UploadFiles.new