This blog is part of our Rails 6 series.
Rails 6 raises ActiveModel::MissingAttributeError when update_columns is used with a non-existing attribute. Before Rails 6, update_columns raised an ActiveRecord::StatementInvalid error.
Rails 5.2
1 2> > User.first.update_columns(email: '[email protected]') 3> > SELECT "users".\* FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]] 4UPDATE "users" SET "email" = $1 WHERE "users"."id" = \$2 [["email", "[email protected]"], ["id", 1]] 5 6=> Traceback (most recent call last): 71: from (irb):8 8ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column "email" of relation "users" does not exist) 9LINE 1: UPDATE "users" SET "email" = $1 WHERE "users"."id" = $2 10^ 11: UPDATE "users" SET "email" = $1 WHERE "users"."id" = $2
Rails 6.0.0.beta2
1 2> > User.first.update_columns(email: '[email protected]') 3> > SELECT "users".\* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]] 4 5Traceback (most recent call last): 61: from (irb):1 7ActiveModel::MissingAttributeError (can't write unknown attribute `email`)
Here is the relevant commit.