2012-09-10 32 views
-1

當我做複製到一些變量後科拉姆值一些邏輯,在對象上我的實際科拉姆價值得到改變,我的模型方法,Rails的模型邏輯給出錯誤的結果

def copy_configuration_values 
    element_positions_dup = element_positions.dup 
    cert_element.read_attribute(:field_names)["configuration_values"].each { |k, v| 
     element_positions_dup["configuration_values"][k] = v if configuration_value_present?(k) 
    } 
    element_positions_dup 
    end 

    def configuration_value_present?(configuration) 
    element_positions["configuration_values"] && element_positions["configuration_values"][configuration] 
    end 

當我從下面的控制檯調用此方法,

1.9.3p194 :001 > t = CertTemplate.find 30 
CertTemplate Load (0.3ms) SELECT `cert_templates`.* FROM `cert_templates` WHERE `cert_templates`.`id` = 30 LIMIT 1 
=> #<CertTemplate id: 30, name: "aaaaaaaaaaaq", cert_element_id: 22, element_positions: {"configuration_values"=>{"Program"=>"9.523810492621529,24.627720154437824"}, "custom_fields"=>{"college"=>"22.64550296843998,15.349369638973906", "code"=>"16.79,15.463920671915272"}, "custom_fields_for_rows"=>{"subject name"=>"30.68783230251736,16.609393247624034"}}, created_at: "2012-08-08 07:18:33", updated_at: "2012-08-16 08:03:52", image_file_name: "Marksheet_Updated.jpg", image_content_type: "image/jpeg", image_file_size: 2236497, image_updated_at: "2012-08-08 07:18:33"> 
1.9.3p194 :002 > 
1.9.3p194 :003 > t.copy_configuration_values 
CertElement Load (0.2ms) SELECT `cert_elements`.* FROM `cert_elements` WHERE `cert_elements`.`id` = 22 LIMIT 1 
=> {"configuration_values"=>{"Program"=>"2"}, "custom_fields"=>{"college"=>"22.64550296843998,15.349369638973906", "code"=>"16.79,15.463920671915272"}, "custom_fields_for_rows"=>{"subject name"=>"30.68783230251736,16.609393247624034"}} 
1.9.3p194 :004 > 
1.9.3p194 :005 > t 
=> #<CertTemplate id: 30, name: "aaaaaaaaaaaq", cert_element_id: 22, element_positions: {"configuration_values"=>{"Program"=>"2"}, "custom_fields"=>{"college"=>"22.64550296843998,15.349369638973906", "code"=>"16.79,15.463920671915272"}, "custom_fields_for_rows"=>{"subject name"=>"30.68783230251736,16.609393247624034"}}, created_at: "2012-08-08 07:18:33", updated_at: "2012-08-16 08:03:52", image_file_name: "Marksheet_Updated.jpg", image_content_type: "image/jpeg", image_file_size: 2236497, image_updated_at: "2012-08-08 07:18:33"> 
1.9.3p194 :006 > 

我的實際列值發生了變化,我做錯了什麼。提前謝謝。

+0

什麼是'element_positions'變量? – AJcodez

+0

這是一個散列。例如:{「configuration_values」=> {「Program」=>「9.523810492621529,24.627720154437824」},「custom_fields」=> {「college」=>「22.64550296843998,15.349369638973906」,「code」=>「16.79,15.463920671915272」}, 「custom_fields_for_rows」=> {「subject name」=>「30.68783230251736,16.609393247624034」}} – Mohanraj

回答

1

看起來問題是,當您遍歷鍵值而不是副本時,您正在分配對嵌套散列的引用。

特別是,element_positionselement_positions_dup中的「custom_fields」鍵將指向與該值相同的Hash對象,因爲您將其指定爲不重複該值。要修復它試圖

... 
element_positions_dup["configuration_values"][k] = v.dup if configuration_value_present?(k) 
... 

編輯:是的,你需要深拷貝

Use Marshal serialization

+0

感謝您的回覆。我曾嘗試使用v.dup,但原始散列正在改變。 – Mohanraj

+0

嗨,我使用了deep_dup「element_positions_dup = element_positions.deep_dup」。 – Mohanraj