Skip to content

Instantly share code, notes, and snippets.

@estolfo
Last active July 4, 2017 13:34
# Updating via the setter. Ignores:
2.2.3 :003 > Child.readonly_attributes
=> #<Set: {"name"}>
2.2.3 :005 > c = Child.new(name: 'emily')
=> #<Child id: nil, mother_id: nil, created_at: nil, updated_at: nil, name: "emily", age: nil>
2.2.3 :006 > c.save
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "children" ("created_at", "updated_at", "name") VALUES (?, ?, ?) [["created_at", "2017-07-04 13:09:49.648017"], ["updated_at", "2017-07-04 13:09:49.648017"], ["name", "emily"]]
(1.6ms) commit transaction
=> true
2.2.3 :007 > c.name = "em"
=> "em"
2.2.3 :008 > c.save
(0.2ms) begin transaction
SQL (0.4ms) UPDATE "children" SET "updated_at" = ? WHERE "children"."id" = ? [["updated_at", "2017-07-04 13:10:29.451684"], ["id", 17]]
(1.5ms) commit transaction
=> true
2.2.3 :009 > c.reload.name
Child Load (0.2ms) SELECT "children".* FROM "children" WHERE "children"."id" = ? LIMIT ? [["id", 17], ["LIMIT", 1]]
=> "emily"
# Increment. Does the operation:
2.2.3 :014 > Child.readonly_attributes
=> #<Set: {"age", "name"}>
2.2.3 :011 > c = Child.create(age:1)
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "children" ("created_at", "updated_at", "age") VALUES (?, ?, ?) [["created_at", "2017-07-04 13:12:28.415277"], ["updated_at", "2017-07-04 13:12:28.415277"], ["age", 1]]
(1.3ms) commit transaction
=> #<Child id: 19, mother_id: nil, created_at: "2017-07-04 13:12:28", updated_at: "2017-07-04 13:12:28", name: nil, age: 1>
2.2.3 :012 > c.increment!(:age)
SQL (2.0ms) UPDATE "children" SET "age" = COALESCE("age", 0) + 1 WHERE "children"."id" = ? [["id", 19]]
=> #<Child id: 19, mother_id: nil, created_at: "2017-07-04 13:12:28", updated_at: "2017-07-04 13:12:28", name: nil, age: 2>
2.2.3 :013 > c.reload.age
Child Load (0.3ms) SELECT "children".* FROM "children" WHERE "children"."id" = ? LIMIT ? [["id", 19], ["LIMIT", 1]]
=> 2
# Updating via []=. Ignores:
2.2.3 :016 > c = Child.create(name: 'emily')
(0.1ms) begin transaction
SQL (0.5ms) INSERT INTO "children" ("created_at", "updated_at", "name") VALUES (?, ?, ?) [["created_at", "2017-07-04 13:16:10.624797"], ["updated_at", "2017-07-04 13:16:10.624797"], ["name", "emily"]]
(1.4ms) commit transaction
=> #<Child id: 20, mother_id: nil, created_at: "2017-07-04 13:16:10", updated_at: "2017-07-04 13:16:10", name: "emily", age: nil>
2.2.3 :017 > c[:name] = 'em'
=> "em"
2.2.3 :018 > c.save
(0.1ms) begin transaction
SQL (1.2ms) UPDATE "children" SET "updated_at" = ? WHERE "children"."id" = ? [["updated_at", "2017-07-04 13:16:17.441827"], ["id", 20]]
(1.4ms) commit transaction
=> true
2.2.3 :019 > c.reload.name
Child Load (0.2ms) SELECT "children".* FROM "children" WHERE "children"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
=> "emily"
# write_attribute. Ignores:
2.2.3 :020 > c = Child.create(name: 'emily')
(0.1ms) begin transaction
SQL (0.4ms) INSERT INTO "children" ("created_at", "updated_at", "name") VALUES (?, ?, ?) [["created_at", "2017-07-04 13:17:36.634634"], ["updated_at", "2017-07-04 13:17:36.634634"], ["name", "emily"]]
(1.5ms) commit transaction
=> #<Child id: 21, mother_id: nil, created_at: "2017-07-04 13:17:36", updated_at: "2017-07-04 13:17:36", name: "emily", age: nil>
2.2.3 :021 > c.write_attribute(:name, 'em')
=> "em"
2.2.3 :022 > c.save
(0.1ms) begin transaction
SQL (0.6ms) UPDATE "children" SET "updated_at" = ? WHERE "children"."id" = ? [["updated_at", "2017-07-04 13:17:49.977649"], ["id", 21]]
(1.6ms) commit transaction
=> true
2.2.3 :023 > c.reload.name
Child Load (0.2ms) SELECT "children".* FROM "children" WHERE "children"."id" = ? LIMIT ? [["id", 21], ["LIMIT", 1]]
=> "emily"
# update_attributes ignores:
2.2.3 :025 > c.update_attributes(:name => 'em')
(0.1ms) begin transaction
SQL (0.4ms) UPDATE "children" SET "updated_at" = ? WHERE "children"."id" = ? [["updated_at", "2017-07-04 13:18:56.974058"], ["id", 21]]
(1.5ms) commit transaction
=> true
2.2.3 :026 > c.reload.name
Child Load (0.2ms) SELECT "children".* FROM "children" WHERE "children"."id" = ? LIMIT ? [["id", 21], ["LIMIT", 1]]
=> "emily"
# update_attributes! ignores:
2.2.3 :027 > c.update_attributes!(:name => 'em')
(0.2ms) begin transaction
SQL (0.4ms) UPDATE "children" SET "updated_at" = ? WHERE "children"."id" = ? [["updated_at", "2017-07-04 13:19:31.403373"], ["id", 21]]
(2.2ms) commit transaction
=> true
2.2.3 :028 > c.reload.name
Child Load (0.2ms) SELECT "children".* FROM "children" WHERE "children"."id" = ? LIMIT ? [["id", 21], ["LIMIT", 1]]
=> "emily"
# update_attribute raises an error:
2.2.3 :029 > c.update_attribute(:name, 'em')
ActiveRecord::ActiveRecordError: name is marked as readonly
from (irb):29
# remove_attribute doesn't exist in ActiveRecord
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment