<commit_before>---
layout: post
title: It's strange - Why I can not update data with hstore on Rails
---
Yesterday, my friend asks me about his problem. He said that he updated data, other fields worked correctly but one field was not updated. He could not understand why it happened.

Luckily, I know this case, my field's type is hstore. When I change the value for a key, then I call save. It does not work. First, here is my record.

```
    #<OrderSubItem id: 178801, qty: 35, production_data: {"size"=>"008", "barcode"=>"4183013003", "dev_size"=>"M", "barcode_type"=>"128 C"}, order_item_id: 35966, sub_item_id: "NPDVR-HT001.001", created_at: "2016-06-03 07:59:51", updated_at: "2016-06-03 07:59:51", minimum_ordering_qty: 21, range: 60000, perc_extra_qty: 0.0, initial_qty: 35, sub_item_site_id: nil, production_nb: nil, pipeline_row_id: 594411, prod_data_sync_flag: false>
```

The field which I want to talk about is production_data. Its type is hstore.

```
    [4] pry(main)> osi.production_data
    => {"size"=>"008",
     "barcode"=>"4183013003",
     "dev_size"=>"M",
     "barcode_type"=>"128 C"}
```

Now our job is updating "size" from "008" to "001". It's very simple, right?

```
    [16] pry(main)> osi.production_data["size"] = "001"
    => "001"
    [17] pry(main)> osi.save
       (0.4ms)  BEGIN
      OrderItem Load (0.5ms)  SELECT  "order_items".* FROM "order_items"  WHERE "order_items"."id" = $1 LIMIT 1  [["id", 35966]]
      SubItem Load (0.4ms)  SELECT  "sub_items".* FROM "sub_items"  WHERE "sub_items"."subitem_code" = $1 LIMIT 1  [["subitem_code", "NPDVR-HT001.001"]]<commit_msg>Correct word<commit_after>---
layout: post
title: It's strange - Why I can not update data with hstore on Rails
---
Yesterday, my friend asked me about his problem. He said that he updated data, other fields worked correctly but one field was not updated. He could not understand why it happened.

Luckily, I know this case, my field's type is hstore. When I change the value for a key, then I call save. It does not work. First, here is my record.

```
    #<OrderSubItem id: 178801, qty: 35, production_data: {"size"=>"008", "barcode"=>"4183013003", "dev_size"=>"M", "barcode_type"=>"128 C"}, order_item_id: 35966, sub_item_id: "NPDVR-HT001.001", created_at: "2016-06-03 07:59:51", updated_at: "2016-06-03 07:59:51", minimum_ordering_qty: 21, range: 60000, perc_extra_qty: 0.0, initial_qty: 35, sub_item_site_id: nil, production_nb: nil, pipeline_row_id: 594411, prod_data_sync_flag: false>
```

The field which I want to talk about is production_data. Its type is hstore.

```
    [4] pry(main)> osi.production_data
    => {"size"=>"008",
     "barcode"=>"4183013003",
     "dev_size"=>"M",
     "barcode_type"=>"128 C"}
```

Now our job is updating "size" from "008" to "001". It's very simple, right?

```
    [16] pry(main)> osi.production_data["size"] = "001"
    => "001"
    [17] pry(main)> osi.save
       (0.4ms)  BEGIN
      OrderItem Load (0.5ms)  SELECT  "order_items".* FROM "order_items"  WHERE "order_items"."id" = $1 LIMIT 1  [["id", 35966]]
      SubItem Load (0.4ms)  SELECT  "sub_items".* FROM "sub_items"  WHERE "sub_items"."subitem_code" = $1 LIMIT 1  [["subitem_code", "NPDVR-HT001.001"]]
{'fraction_non_alphanumeric': 0.151985111662531, 'fraction_numerical': 0.08312655086848635, 'mean_word_length': 4.201612903225806, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6433329', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1175, 'n_words': 378}