2013-10-17 29 views
-1

當我使用after_commit過濾我才意識到,這似乎是兩次執行函數(未知的原因對我來說)爲什麼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

假設我明白你的意思是正確的,你在控制器的create操作中做了多次保存,因爲UploadFiles.create@upload_files.save都會觸發數據庫操作。

也許你的意思是UploadFiles.new

+0

謝謝你,我找不到這樣做的原因。我是紅寶石和鐵軌的新手。我的錯 – Splendonia

相關問題