ActiveRecord What is ActiveRecord It is an ORM (Object RDB Mapper) adopted by Rails.
It translates from Ruby to SQL between Model and DB.
Basically, SQL is used as the DB language for DB, DB operation is not possible without SQL, Thanks to ActiveRecord applied to Model in Rails, you can search for and bring data from DB using Ruby. (Strictly speaking, ActiveRecord is applied to Model via ApplicationRecord)
Looking at the Model, I think it looks like the following. this is Inherit ActiveRecord to ApplicationRecord It means that it inherits ApplicationRecord to other models such as user.rb.
By the way, I don't know the detailed meaning of self.abstract_class = true, The necessity and significance are as follows.
When you create a class that inherits ActiveRecord :: Base as a model, Rails will automatically try to find the database table for that class name. If you don't have a corresponding database table, you need to write self.abstract_class = true. So If you want to create a class that inherits ActiveRecord :: Base and want to inherit that class, you need to write self.abstract_class = true.
class ApplicationRecord < ActiveRecord::Base self.abstract_class = true end
class User < ApplicationRecord end
Representing the model and the data within the model Representing the association between models Represents an inheritance hierarchy between associated models Validate the data before persisting it in the database Operate the database in an object-oriented style Intuitively write in Ruby You can write in Ruby in the same way no matter which DB you are using
CRUD CRUD is an acronym for "Create," "Read," "Update," and "Delete," which stands for four database operations. Active Record automatically creates these methods so that your application can work with the data stored in the table.
Create (create) Read (display) Update (update) Delete (delete)
The methods that the model can use for table operations. Used to store and retrieve information in tables. I will introduce the main methods in CRUD order.
|new||Create an instance (record) of the class|
|build||Create an instance (record) of the class|
|save||Save an instance (record) of a class|
|create||Create an instance (record) of the class and save it in the DB at the same time.|
|all||Get all records as an array from the table|
|first||Get the record with the lowest id in the table|
|last||Get the record with the highest id in the table|
|find||Find the record with the same id as the number contained in the argument|
|find_by||Get the record with the youngest id that meets the conditions contained in the argument|
|where||Get all records that meet the conditions contained in the argument|
|order||Sort the columns in the argument to: asc (ascending) or: desc (descending) to get the records|
|get only the column with the name contained in the select||argument|
|limit||Get records in ascending order of id by the number of numbers contained in the argument|
|update||Update based on the record obtained by find or find_by|
|update_by||Update multiple records at once|
|update_all||Update all records at once|
|destroy||Delete records obtained by find or find_by|
|destroy_by||Delete multiple records at once|
|destroy_all||Delete all records at once|
For this article, I referred to the following two articles. https://railsguides.jp/active_record_basics.html#active-record%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6