ActiveRecordの継承クラスの属性やカラムの一覧を取得する

### 前提条件

Rails: 5.2
Ruby: 2.5.1

# 本題
サンプルのユーザテーブルの定義

```
create_table :users do |t|
  t.string :name
  t.timestamps
end
```

ユーザモデルの定義

```
class User < ApplicationRecord
end
```

## クラスから一覧を取得する場合

いくつかパターンがあるが、概ね`column_names`メソッドで事足りるだろう

```
User.column_names
=>  ["id", "name", "created_at", "updated_at"]

User.columns
=> [#<ActiveRecord::ConnectionAdapters::Column:0x00007fefd51da6b0 @name="id", @table_name="users", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007fefd51e87b0 @sql_type="integer", @type=:integer, @limit=nil, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function=nil, @collation=nil, @comment=nil>, #<ActiveRecord::ConnectionAdapters::Column:0x00007fefd51d9da0 @name="name", @table_name="users", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007fefd51d9e40 @sql_type="varchar", @type=:string, @limit=nil, @precision=nil, @scale=nil>, @null=true, @default=nil, @default_function=nil, @collation=nil, @comment=nil>, #<ActiveRecord::ConnectionAdapters::Column:0x00007fefd51d9418 @name="created_at", @table_name="users", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007fefd51d9468 @sql_type="datetime", @type=:datetime, @limit=nil, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function=nil, @collation=nil, @comment=nil>, #<ActiveRecord::ConnectionAdapters::Column:0x00007fefd51d92b0 @name="updated_at", @table_name="users", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007fefd51d9300 @sql_type="datetime", @type=:datetime, @limit=nil, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function=nil, @collation=nil, @comment=nil>]


User.columns_hash
=> {"id"=>#<ActiveRecord::ConnectionAdapters::Column:0x00007fefd51da6b0 @name="id", @table_name="users", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007fefd51e87b0 @sql_type="integer", @type=:integer, @limit=nil, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function=nil, @collation=nil, @comment=nil>, "name"=>#<ActiveRecord::ConnectionAdapters::Column:0x00007fefd51d9da0 @name="name", @table_name="users", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007fefd51d9e40 @sql_type="varchar", @type=:string, @limit=nil, @precision=nil, @scale=nil>, @null=true, @default=nil, @default_function=nil, @collation=nil, @comment=nil>, "created_at"=>#<ActiveRecord::ConnectionAdapters::Column:0x00007fefd51d9418 @name="created_at", @table_name="users", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007fefd51d9468 @sql_type="datetime", @type=:datetime, @limit=nil, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function=nil, @collation=nil, @comment=nil>, "updated_at"=>#<ActiveRecord::ConnectionAdapters::Column:0x00007fefd51d92b0 @name="updated_at", @table_name="users", @sql_type_metadata=#<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x00007fefd51d9300 @sql_type="datetime", @type=:datetime, @limit=nil, @precision=nil, @scale=nil>, @null=false, @default=nil, @default_function=nil, @collation=nil, @comment=nil>}       

```

## インスタンスから一覧を取得する場合

こちらも一覧を撮ってくるという用途であれば概ねattribute_namesで良さそうだ。

```
user = User.new
user.attribute_names
=> ["id", "name", "created_at", "updated_at"]

user.attributes
=> {"id"=>nil, "name"=>nil, "created_at"=>nil, "updated_at"=>nil}
```