Created
January 9, 2015 08:55
-
-
Save rob-murray/9d9eb63795b00e8ba9d4 to your computer and use it in GitHub Desktop.
Update a Rails readonly field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 2.1.5 :020 > User | |
| => User(id: integer, name: string, email: string, created_at: datetime, updated_at: datetime) | |
| 2.1.5 :021 > User.create(name: 'rob', email: '[email protected]') | |
| (0.1ms) begin transaction | |
| SQL (1.0ms) INSERT INTO "users" ("name", "email", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "rob"], ["email", "[email protected]"], ["created_at", "2015-01-09 08:50:31.962223"], ["updated_at", "2015-01-09 08:50:31.962223"]] | |
| (0.8ms) commit transaction | |
| => #<User id: 2, name: "rob", email: "[email protected]", created_at: "2015-01-09 08:50:31", updated_at: "2015-01-09 08:50:31"> | |
| 2.1.5 :022 > User.first | |
| User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 | |
| => #<User id: 2, name: "rob", email: "[email protected]", created_at: "2015-01-09 08:50:31", updated_at: "2015-01-09 08:50:31"> | |
| 2.1.5 :023 > | |
| 2.1.5 :024 > User.first.update(email: '[email protected]') | |
| User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 | |
| (0.1ms) begin transaction | |
| SQL (1.4ms) UPDATE "users" SET "updated_at" = ? WHERE "users"."id" = ? [["updated_at", "2015-01-09 08:51:20.188838"], ["id", 2]] | |
| (1.0ms) commit transaction | |
| => true | |
| 2.1.5 :025 > User.first | |
| User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 | |
| => #<User id: 2, name: "rob", email: "[email protected]", created_at: "2015-01-09 08:50:31", updated_at: "2015-01-09 08:51:20"> | |
| 2.1.5 :026 > User.class_eval do | |
| 2.1.5 :027 > def self.readonly_attributes | |
| 2.1.5 :028?> [] | |
| 2.1.5 :029?> end | |
| 2.1.5 :030?> end | |
| => :readonly_attributes | |
| 2.1.5 :031 > | |
| 2.1.5 :032 > User.first.update(email: '[email protected]') | |
| User Load (0.4ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 | |
| (0.1ms) begin transaction | |
| SQL (0.4ms) UPDATE "users" SET "email" = ?, "updated_at" = ? WHERE "users"."id" = ? [["email", "[email protected]"], ["updated_at", "2015-01-09 08:53:32.479937"], ["id", 2]] | |
| (2.8ms) commit transaction | |
| => true | |
| 2.1.5 :033 > User.first | |
| User Load (0.4ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 | |
| => #<User id: 2, name: "rob", email: "[email protected]", created_at: "2015-01-09 08:50:31", updated_at: "2015-01-09 08:53:32"> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # == Schema Information | |
| # | |
| # Table name: users | |
| # | |
| # id :integer not null, primary key | |
| # name :string | |
| # email :string | |
| # created_at :datetime not null | |
| # updated_at :datetime not null | |
| # | |
| class User < ActiveRecord::Base | |
| attr_readonly :email | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment