DEV Community

@kon_yu
@kon_yu

Posted on

Get the list of attributes and columns of the inheritance class of ActiveRecord

prerequisite

Rails: 5.2
Ruby: 2.5.1

main topic

Sample User Table Definitions

create_table :users do |t|
  t.string :name
  t.timestamps
end
Enter fullscreen mode Exit fullscreen mode

Defining the user model

class User < ApplicationRecord
end
Enter fullscreen mode Exit fullscreen mode

When retrieving a list from a class

There are several patterns, but mostly the column_names method will suffice.

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>}       

Enter fullscreen mode Exit fullscreen mode

When retrieving a list from an instance

If you want to take a list, you can use 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}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)