Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

前提条件

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}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした