Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

10
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsのコンソールで、モデルのカラム名一覧をシンボル型の配列で取得する

Last updated at Posted at 2019-01-31

カラムが大量にあるときに一括でストロングパラメーターを設定する場合などに、コピペすると便利。
(2/2 修正)scivola様より、改善いたしました。.inspectは、改行を防ぐためです。

コンソール
[1] pry(main)> モデル名.column_names.map(&:to_sym).inspect

#=> "[:id, :user_id, :name, ...]"
10
2
8

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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
10
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?