2011-05-13 60 views
1
def update 
    @product_category = @business_category.product_categories.find(params[:id]) 
    product_category_was = @business_category.product_categories.find(params[:id]) 

    respond_to do |format| 
     if @product_category.update_attributes(params[:product_category]) 
     share_associations(@product_category, product_category_was) if in_params('_maps_attributes', 'product_category') 

     format.js 
     format.html { redirect_to(admin_product_categories_path, :notice => 'Product category was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.js 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @product_category.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

函數share_associations具有參數@product_category和product_category_was。問題是,當我打電話給product_category_was.send('圖像')例如(我必須使用發送調用,因爲調用是動態的),它顯然會拉出最新的相關圖像,而不是相關的圖像。無論如何,我可以讓對象獲得與它製作時相關的圖像嗎?如何在Rails 3中的控制器中獲得before_save值?

+0

請具體 – Hitesh 2011-05-13 07:25:57

+0

可能需要查看模型代碼以瞭解您在做什麼。 – 2011-05-13 08:46:02

回答

0

我認爲你需要你的對象的深拷貝,因爲正常協會(=)將只創建一個參考:

product_category_was = Marshal::load(Marshal::dump(@product_category)) 

這可能不適用於所有類型的對象的工作,但是對於普通的Rails這應該工作的對象。

0

我不知道arnep在說什麼,也不知道你會遇到什麼問題。你在做什麼對我來說通過一個協會有兩種不同的發現,所以它應該如此。

irb(main):016:0> s = School.first 
=> #<School id: 2, name: "Bar", created_at: "2011-04-09 17:48:57", updated_at: "2011-05-13 09:13:38", confirmed: nil, zipcode: nil> 
irb(main):017:0> g1 = s.grades.find 4 
=> #<Grade id: 4, name: "4th", type: nil, school_id: 2, created_at: "2011-04-19 03:17:49", updated_at: "2011-05-13 09:15:17"> 
irb(main):018:0> g2 = s.grades.find 4 
=> #<Grade id: 4, name: "4th", type: nil, school_id: 2, created_at: "2011-04-19 03:17:49", updated_at: "2011-05-13 09:15:17"> 
irb(main):019:0> g1.update_attributes :name => '5th' 
=> true 
irb(main):020:0> g2 
=> #<Grade id: 4, name: "4th", type: nil, school_id: 2, created_at: "2011-04-19 03:17:49", updated_at: "2011-05-13 09:15:17"> 
irb(main):021:0> g1 
=> #<Grade id: 4, name: "5th", type: nil, school_id: 2, created_at: "2011-04-19 03:17:49", updated_at: "2011-05-13 09:16:02"> 
irb(main):022:0> 

事實上,通常人們都在問逆問題 - 如何讓一個已經實例化對象從數據庫重新加載。該問題可能在您的share_associations方法中,或者您尚未顯示的其他內容。

0

我找到了一種方法來做一些現在可行的工作。這不是最好的方式,但它現在起作用。我基本上創建了一個空數組,並將product_categories數組放入其中。這使得價值不再是一個呼叫,所以價值不會改變。希望這會最終幫助別人。

def update 
    @product_category = @business_category.product_categories.find(params[:id]) 
    if in_params('_maps_attributes', 'product_category') 
     media_was = Array.new 
     media_was = media_was | @business_category.product_categories.find(params[:id]).send(map_type('product_category').pluralize) 
    end 

    respond_to do |format| 
     if @product_category.update_attributes(params[:product_category]) 
     share_associations(@product_category, media_was) if in_params('_maps_attributes', 'product_category') 

     format.js 
     format.html { redirect_to(admin_product_categories_path, :notice => 'Product category was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.js 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @product_category.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 
相關問題