B413 B413 - 1 year ago 99
Ruby Question

Update a value into a JSON string with Ruby ActiveRecord?

Sorry to ask this question but I'm really newbie with Ruby and I need help to update several records on mu database.

Starting with the code below. Could you help me to write a console query or instruction that help me to change the value of the "size"=>"001" to "size"=>"400".

2.1.2 :050 > orders.first
=> #<Order id: 3193, qty: 3193, data: {"size"=>"001", "barcode"=>"123456789", "barcode_type"=>"128"}, order_item_id: 473, sub_item_id: 001, created_at: "2015-07-26 05:03:32", updated_at: "2015-07-26 05:03:32">

Then what should I do if I want to update all orders, not only the first one?

Answer Source

After testing the solution of M Kumar I found the correct way to do it is

orders = Order.all
orders.each do |order|
  order.data["size"] = "400"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download