當我做複製到一些變量後科拉姆值一些邏輯,在對象上我的實際科拉姆價值得到改變,我的模型方法,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 >
我的實際列值發生了變化,我做錯了什麼。提前謝謝。
什麼是'element_positions'變量? – AJcodez
這是一個散列。例如:{「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