Rails JSON, Postgres , JSON. Postgres JSON, , , . Rails 4.1.9 .
Ruby 2.1.0p0, Rails 4.1.9, Postgres 9.4.4:
add_column :things, :data, :json, default: []
> t = Thing.new
=> #<Thing id: nil, data: []>
> t.data = [{a: 1}]
=> [{:a=>1}]
> t.save
(0.2ms) BEGIN
SQL (0.7ms) INSERT INTO "things" ("data") VALUES ($1) RETURNING "id" [["data", "[{\"a\":1}]"]]
(0.5ms) COMMIT
=> true
> Thing.first
Thing Load (1.0ms) SELECT "things".* FROM "things" ORDER BY "things"."id" ASC LIMIT 1
=> #<Thing id: 1, data: [{"a"=>1}]>
, Rails , . , . :
> t = Thing.create
(0.2ms) BEGIN
SQL (0.4ms) INSERT INTO "things" DEFAULT VALUES RETURNING "id"
(4.7ms) COMMIT
=> #<Thing id: 2, data: []>
> t.data << 1
=> [1]
> t.save
(0.2ms) BEGIN
(0.2ms) COMMIT
=> true
> t.reload.data
Thing Load (0.3ms) SELECT "things".* FROM "things" WHERE "things"."id" = $1 LIMIT 1 [["id", 2]]
=> []
save
, , , . , , *_will_change!
Rails, .
> t.data << 1
=> [1]
> t.data_will_change!
=> [1]
> t.save
(0.2ms) BEGIN
SQL (0.4ms) UPDATE "things" SET "data" = $1 WHERE "things"."id" = 2 [["data", "[1]"]]
(0.4ms) COMMIT
=> true
> t.reload.data
Thing Load (0.4ms) SELECT "things".* FROM "things" WHERE "things"."id" = $1 LIMIT 1 [["id", 2]]
=> [1]