ActiveRecord does not save attribute after update

I am wondering if there is something here that I do not understand, or if I encounter an error in ActiveRecord (4.1.1).

I have a database full of records with only one attribute in the JSON bit field. I take it and try to update it like this.

test = Submission.find(1)
test.update_attribute('json_data',similar_but_different_json(test.json_data))

Suppose the method similar_but_different_jsonmakes a small update for this JSON. In my case, I fix some data errors that were created by a broken form.

However, I do not get any errors, I show the commit in the console, but the data is not sent or returned true.

To really update the record, I have to do this.

test = Submission.find(1)
old_json_data = test.json_data
test.json_data = ""
test.json_data = similar_but_different_json(old_json_data)
test.save

, -, , , ActiveRecord , , . , , JSON ?

+4
3

will_change!

:

test.json_data_will_change!   # Goes before the save.

ActiveModel, json_data (.. ← - ) .

. Rails , , .

+8

, . update_columns:

test.update_columns(json_data: similar_but_different_json(test.json_data))

UPDATE , - , .. json_data .

+1

ActiveModel ( 4.1.1) "" .

'similar_but_different_json', , .

.

test = Submission.find(1)
test_json_data_duplicate = test.json_data.dup 
test.update_attribute('json_data',similar_but_different_json(test_json_data_duplicate))

...

test.json_data = ""

... ActiveModel , String, . , update_attribute, , .

If you try to clear the line in an integrated way, the trick will not work.

test = Submission.find(1)
old_json_data = test.json_data
test.json_data.clear # Instead of test.json_data = ""
test.json_data = similar_but_different_json(old_json_data)
test.save

ActiveModel :: Dirty

+1
source

Source: https://habr.com/ru/post/1540047/


All Articles