-1

When i do some logic on a colum value after copied to some variable, my actual colum value on the object is getting changed, my model methods are,

  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

And when i call this method from console like below,

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.790123349144345,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.790123349144345,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.790123349144345,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 >

My actual column value is getting changed, what i am doing wrong. Advance thanks.

georgebrock
  • 28,393
  • 13
  • 77
  • 72
Mohanraj
  • 4,056
  • 2
  • 22
  • 24
  • What is `element_positions` variable? – AJcodez Sep 10 '12 at 09:26
  • It is an hash. Ex: {"configuration_values"=>{"Program"=>"9.523810492621529,24.627720154437824"}, "custom_fields"=>{"college"=>"22.64550296843998,15.349369638973906", "code"=>"16.790123349144345,15.463920671915272"}, "custom_fields_for_rows"=>{"subject name"=>"30.68783230251736,16.609393247624034"}} – Mohanraj Sep 10 '12 at 09:47

1 Answers1

1

It looks like the problem is you are assigning references to nested hashes when you iterate through the key values, instead of copies.

Like specifically, "custom_fields" key in both element_positions and element_positions_dup will point to the same Hash object as the value because you assign it without duplicating it. To fix it try

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

Edit: yeah you need deep copies

Use Marshal serialization

Community
  • 1
  • 1
AJcodez
  • 31,780
  • 20
  • 84
  • 118