March 20, 2019
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.
> > User.first.update_columns(email: '[email protected]')
> > SELECT "users".\* FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
UPDATE "users" SET "email" = $1 WHERE "users"."id" = \$2 [["email", "[email protected]"], ["id", 1]]
=> Traceback (most recent call last):
1: from (irb):8
ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR: column "email" of relation "users" does not exist)
LINE 1: UPDATE "users" SET "email" = $1 WHERE "users"."id" = $2
^
: UPDATE "users" SET "email" = $1 WHERE "users"."id" = $2
> > User.first.update_columns(email: '[email protected]')
> > SELECT "users".\* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
Traceback (most recent call last):
1: from (irb):1
ActiveModel::MissingAttributeError (can't write unknown attribute `email`)
Here is the relevant commit.
If this blog was helpful, check out our full blog archive.