[RUBY] Get the column name from the Model instance

I thought it would be nice if there was a key name list for display when creating the list screen.

procedure

1. Model creation

Create some model

models/user.rb


class User < ApplicationRecord
  include Table #If you include it here, you can use it as an instance method

  attribute :name
  attribute :email
  attribute :login_id
  attribute :password

end

2. Create a method to get the column name from the Model instance

models/concerns/table.rb


module Table
  extend ActiveSupport::Concern

  #Get column name (symbol) as an array
  def table_keys
    self.class.column_names.map(&:to_sym).reject { |column| %i[id created_at updated_at].include?(column) }

    #Click here if you want to get it as a character string
    # self.class.column_names.reject { |column| %w[id created_at updated_at].include?(column) }
  end

  #Get column name
  def col_name(column_name)
    self.class.human_attribute_name(column_name)
  end
end
  1. Get ʻUser from @ useretc. withself.class`
  2. Get the column name list with column_names.map (&: to_sym) and convert it to a symbol
  3. reject { |column| %w[id created_at updated_at].include?(column) }Exclude unnecessary column names in

3. Display on the list screen

ruby:views/users/index.html.slim



- if @users.present?
  table.table
    thead
      tr
        - first_user = @users.first
        - first_user.table_keys.each do |key|
          = tag.td first_user.col_name(key)
    tbody
      - @users.each do |user| 
        tr
          - user.table_keys.each do |key|
            = tag.td {|tag| tag.span user.send(key)}
- else
p No data


I was able to display it like this. (I use bootstrap) ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/554638/22731714-2129-b0bd-89a1-b4aa9048e747.png)

Finally

This kind of screen is often used for devise and active_admin, but it seems that it can also be used when passing the validation key to the front side.

If you have any advice, please do not hesitate to contact us.

Recommended Posts

Get the column name from the Model instance
Get the object name of the instance created by the new operator
[IOS] How to get the table name from AWS DynamoDB
Get "2-4, 7, 9" from [4, 7, 9, 2, 3]
When you get lost in the class name
[JAVA] Get only the file name, excluding the extension
Get Enum by reverse lookup from the contents
Use remotes :: install_github ("github repository name") to get resources from GitHub to the R runtime environment
Enum misconfiguration. For some reason, I can't get data from the column specified by Enum ...
[Rails] Let's dynamically get the threshold of model validation "length check" from table information
[Kotlin] Get the argument name of the constructor by reflection
Get the anime name for this term by scraping
Get the name of the test case in the JUnit test class
[Java] How to extract the file name from the path
[Ruby on Rails] How to change the column name
[Rails] How to change the column name of the table
Ruby, Nokogiri: Get the element name of the selected node
Ruby/Racc: Get the position (row, column) where parsing failed
Refactor the Fat Model
How to get the class name / method name running in Java
How to dynamically change the column name acquired by MyBatis
Get to the abbreviations from 5 examples of iterating Java lists
How to get the longest information from Twitter as of 12/12/2016
[Rough explanation] Causes and remedies for not being able to obtain the name from the ActiveHash model