[RAILS] How to reference a column when overriding the column name method in ActiveRecord

background

I want to define an instance method with the same name as the DB column name in the model and refer to the column in that method.

Method

There seem to be two methods

class Person < ApplicationRecord
  def name
    self[:name]
  end

  def name
    read_attribute(:name)
  end
end

In The Rails Style Guide, self [: name] is better. Recommend

reference

https://github.com/satour/rails-style-guide/blob/master/README-jaJA.md#read-attribute https://stackoverrun.com/ja/q/5968719

Recommended Posts

How to reference a column when overriding the column name method in ActiveRecord
How to get the class name / method name running in Java
How to store the information entered in textarea in a variable in the method
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
[Ruby on Rails] How to change the column name
[Rails] How to change the column name of the table
How to mock a super method call in PowerMock
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to use the getter / setter method (in object orientation)
How to set chrony when the time shifts in CentOS7
How to create a placeholder part to use in the IN clause
Create a method to return the tax rate in Java
How to add the same Indexes in a nested array
How to use the link_to method
How to perform a specific process when the back button is pressed in Android Fragment
How to use the include? method
How to name variables in Java
How to output the value when there is an array in the array
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
Why you should add the Override annotation when overriding a method
Add a time stamp to the JAR file name in Gradle
How to solve the unknown error when using slf4j in Java
How to write the view when Vue is introduced in Rails?
[Java] How to search for a value in an array (or list) with the contains method
Method definition location Summary of how to check When defined in the project and Rails / Gem
How to change app name in rails
Java reference to understand in the figure
How to insert a video in Rails
[Rails] How to use the map method
[Java] How to use the toString () method
How to have params in link_to method
How to test a private method in Java and partially mock that method
How to get the current date as a string in yyyyMMdd format
How to find the total number of pages when paging in Java
How to constrain the action of the transition destination when not logged in
How to change the value of a variable at a breakpoint in intelliJ
How to pass a proxy when throwing REST over SSL in Java
How to get the absolute path of a directory running in Java
[Rails] How to create a table, add a column, and change the column type
[Ruby] How to prevent errors when nil is included in the operation
graphql-ruby: How to get the name of query or mutation in controller Note
How to check the logs in the Docker container
[Rails] How to write when making a subquery
How to display a web page in Java
How to get the ID of a user authenticated with Firebase in Swift
Be careful when setting the class name when creating a document-based app in Swift
When the server fails to start in Eclipse
When you get lost in the class name
How to fix a crash when deleting Realm data in Swift UI List
Call a method of the parent class by explicitly specifying the name in Ruby
When you want to use the method outside
How to delete the database when recreating the application
How to run a djUnit task in Ant
Output of how to use the slice method
How to add a classpath in Spring Boot
How to create a theme in Liferay 7 / DXP
How to make a judgment method to search for an arbitrary character in an array
How to implement a like feature in Rails
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
Isn't there a name collision when enum has the same name in the switch statement?
How to easily create a pull-down in Rails