LoginSignup
6
1

More than 5 years have passed since last update.

hash.exceptの引数をシンボルで指定したら動いてくれなかった

Posted at

表題の通りです。

# user
#  - first_name
#  - last_name
#  - age
#  - email

user.attributes.except(:age,:email)

上記のように、削除したいキーをシンボルで指定したところ、
Hashには全てのキーが含まれたままでした。

attributesはキーを文字列として返すらしい

Stack Overflowで調べてみたところ、
全く同じ問題で躓いている人がいました。

ActiveRecordのattributesは、
キーをシンボルではなく文字列で返すとのこと。
ソウダッタノカ------!

ということで、下記のように修正すると、
Hashから指定したキーが削除されました!

user.attributes.except('age','email')

よかったよかった:innocent:

6
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
1