2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Railsコンソールでモデルのテーブル定義を確認できるようにする

Last updated at Posted at 2025-05-24

はじめに

Ruby on Railsのコンソールでモデルのテーブル定義を確認したかったが、適切なコマンドがなかったので設定した
今回は以下を表示するようにした

  • カラム名
  • PKかどうか
  • notnullかどうか
  • 型と最大桁数を表示するようにした

環境

  • ruby: 3.2.8
  • rails: 7.0.4.3
  • SqlLite: 1.6.1

結論

  • bashでrubyの設定ファイルを開く
$ vim ~/.irbrc
  • irbrcに関数をかく
if defined?(Rails)
  puts '✅ Rails console loaded with show_table'

  def show_table(model)
    model.columns.each do |col|
      puts [
        "name=#{col.name.ljust(15)}",
        "type=#{col.sql_type.ljust(15)}",
        "null=#{col.null ? 'OK'.ljust(10) : 'NOT NULL'.ljust(10)}",
        "primary=#{model.primary_key == col.name}",
        ("limit=#{col.limit}" if col.limit)
      ].compact.join(' | ')
    end
    nil
  end
end

*保存して閉じる

  • railsに接続して実行してみる
$ rails c 
>> show_table(User)
name=id              | type=INTEGER         | null=NOT NULL   | primary=true
name=name            | type=varchar         | null=OK         | primary=false
name=email           | type=varchar         | null=OK         | primary=false
name=created_at      | type=datetime(6)     | null=NOT NULL   | primary=false
name=updated_at      | type=datetime(6)     | null=NOT NULL   | primary=false
name=password_digest | type=varchar         | null=OK         | primary=false
=> nil

解説

model.columns

  • これは通常のコマンド
こんなかんじ(長いので折りたたむ)
>> User.columns
=> 
[#<ActiveRecord::ConnectionAdapters::Column:0x000072fc3c1af2d8
  @collation=nil,
  @comment=nil,
  @default=nil,
  @default_function=nil,
  @name="id",
  @null=false,
  @sql_type_metadata=
   #<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x000072fc3c1aff58
    @limit=nil,
    @precision=nil,
    @scale=nil,
    @sql_type="INTEGER",
    @type=:integer>>,
 #<ActiveRecord::ConnectionAdapters::Column:0x000072fc3c1af0f8
  @collation=nil,
  @comment=nil,
  @default=nil,
  @default_function=nil,
  @name="name",
  @null=true,
  @sql_type_metadata=
   #<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x000072fc3c1af1e8
    @limit=nil,
    @precision=nil,
    @scale=nil,
    @sql_type="varchar",
    @type=:string>>,
 #<ActiveRecord::ConnectionAdapters::Column:0x000072fc3c1aeb08
  @collation=nil,
  @comment=nil,
  @default=nil,
  @default_function=nil,
  @name="email",
  @null=true,
  @sql_type_metadata=
   #<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x000072fc3c1af1e8
    @limit=nil,
    @precision=nil,
    @scale=nil,
    @sql_type="varchar",
    @type=:string>>,
 #<ActiveRecord::ConnectionAdapters::Column:0x000072fc3c1ae428
  @collation=nil,
  @comment=nil,
  @default=nil,
  @default_function=nil,
  @name="created_at",
  @null=false,
  @sql_type_metadata=
   #<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x000072fc3c1ae928
    @limit=nil,
    @precision=6,
    @scale=nil,
    @sql_type="datetime(6)",
    @type=:datetime>>,
 #<ActiveRecord::ConnectionAdapters::Column:0x000072fc3c1ae1f8
  @collation=nil,
  @comment=nil,
  @default=nil,
  @default_function=nil,
  @name="updated_at",
  @null=false,
  @sql_type_metadata=
   #<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x000072fc3c1ae928
    @limit=nil,
    @precision=6,
    @scale=nil,
    @sql_type="datetime(6)",
    @type=:datetime>>,
 #<ActiveRecord::ConnectionAdapters::Column:0x000072fc3c1adf78
  @collation=nil,
  @comment=nil,
  @default=nil,
  @default_function=nil,
  @name="password_digest",
  @null=true,
  @sql_type_metadata=
   #<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x000072fc3c1af1e8
    @limit=nil,
    @precision=nil,
    @scale=nil,
    @sql_type="varchar",
    @type=:string>>]    
  • 一部抜粋
#<ActiveRecord::ConnectionAdapters::Column:0x000072fc3c1af2d8
  @collation=nil,
  @comment=nil,
  @default=nil,
  @default_function=nil, 
  @name="id", 
  @null=false, 
  @sql_type_metadata=
   #<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x000072fc3c1aff58
    @limit=nil,
    @precision=nil,
    @scale=nil,
    @sql_type="INTEGER",
    @type=:integer>>,
  • 上記で1カラムの情報(この場合はidカラムの情報)
  • ConnectionAdapters
    • カラム全体の情報
  • SqlTypeMetadata
    • データ型に関する詳細

欲しい情報の取得

"name=#{col.name.ljust(15)}",

model.columsでカラムをeachで一つずつ参照し、その中から必要なデータ(name)など取得する
ljust(15)で15文字埋めと言う意味。見た目をきれいにするため使用

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?