1
我正在序列化模型页上的许多属性作为散列。rails使用.send()与序列化列添加元素散列
由于属性数量众多,我采用了元编程方法,并希望使用.send()遍历属性集合(这样我就不必输入更新操作对于
每个属性我做了这样的事情:
insights.each do |ins|
self.send("#{ins.name}=", {(Time.now) => ins.values[1]['value'].to_f})
self.save
end
的问题是,这显然将覆盖整个序列化列,而我想添加为一个元素序列化的哈希
。试过类似的东西这样的:
insights.each do |ins|
self.send("#{ins.name}[#{Time.now}]=", ins.values[1]['value'].to_f)
self.save
end
却得到了一个NoMethodError: undefined method page_fan_adds_unique[Mon Aug 13 13:31:58 -0400 2012]=
在我能够做Page.find(5).page_fan_adds_unique [Time.now] = 12345并将其保存为到一个额外的元素控制台散列如预期。
那么如何使用.send()将附加元素保存到序列化哈希?还是有其他方法?如使用update_attribute或其他方法?写我自己的?任何帮助表示赞赏,即使建议是我不应该使用序列化。
'self.send(ins.name)。发送(:[] =,Time.now,ins.values [1] [ '值'] to_f) '在这种情况下更可能工作。 – 2012-08-13 18:25:01
@CalebThompson:是的,可能是,不知道这里的字符串在哪里:) – apneadiving 2012-08-13 18:44:42
直奔@CalebThompson的解决方案,它做到了。谢谢,从来没有想过调用send两次。 – sbauch 2012-08-13 20:07:47