Serialization save problem

This is my class:

class Articulo < ActiveRecord::Base

serialize :atributos_extendidos, Hash

def after_find
self.atributos_extendidos.each do |k,v|
Articulo.class_eval <<-CODE
def #{k}
self.atributos_extendidos ||= {}
self.atributos_extendidos[:#{k}]
end
def #{k}=(val)
self.atributos_extendidos ||= {}
self.atributos_extendidos[:#{k}] = val
end
CODE
end
end
end

And I try this in console:

a=Articulo.new(:atributos_extendidos => {:metros => “2”,:habitaciones => “4”})
a.save!
a
=> #<Articulo id: 5, … , atributos_extendidos: {:metros=>“2”,
:habitaciones=>“4”}>

c=Articulo.find(5)
=> #<Articulo id: 5, … , atributos_extendidos: {:habitaciones=>“4”,
:metros=>“2”} >

c.habitaciones
=> “4”

c.metros
=> “2”

c.metros = “4”
=> “4”

c
=> #<Articulo id: 5, … , atributos_extendidos: {:habitaciones=>“4”,
:metros=>“4”}>

c.save!
=> true

c
=> #<Articulo id: 5, … , atributos_extendidos: {:habitaciones=>“4”,
:metros=>“4”} >

c.reload

=> #<Articulo id: 5, … , atributos_extendidos: {:habitaciones=>“4”,
:metros=>“2”}> # metros returns to “2”

Why I can’t save modified values in :atributos_extendidos? What am I
doing wrong?
Thanks
Juan Matías.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs