[Rails] ActiveRecord

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


class User < ApplicationRecord


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)

ActiveRecord method

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.

○ Create method

Method Usage
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.

○ Read method

Method Use
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 method

Method Use
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

○ Delete method

Method Use
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


Recommended Posts

[Rails] ActiveRecord
[Rails] ActiveRecord :: Attributes :: ClassMethods
Resolve ActiveRecord :: NoDatabaseError on rails6
[Rails g.error]
Rails basics
Rails Review 1
Rails API
Rails migration
[Rails] first_or_initialize
rails tutorial
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
[Rails] devise
rails tutorial
Rails Tips
rails method
rails tutorial
[Rails] form_with
Rails Review 2
Rails 5 Code Reading Part 1 ~ ActiveRecord new Method ~
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
Rails CSV basics
About Rails routing
Rails Routing Basics
Add binding.pry (rails)
[Rails / ActiveRecord] About the difference between create and create!
Rails database basics
Rails access restrictions
Rails and FormData
rails tutorial Chapter 6
Rails tutorial test
Rails render redirect_to
[Rails] Favorite feature
[Rails] Many-to-many creation
[Rails] Naming convention
Rails Logger Basics
[Rails] Introducing jquery
Rails Flash Message
rails tutorial Chapter 1
Rails delegate method
[Rails 6] cocoon_ introduction
[rails] Set validation
Rails learning day 3
Rails tutorial memorandum 2
Rails learning day 4
Implement Rails pagination
About ActiveRecord :: PendingMigrationError
[Rails] Category function
rails tutorial Chapter 7
[Rails] Introducing devise
rails tutorial Chapter 5
Group_by in Rails
Rails follow function
Rails 6.0 Routing Summary
[Rails] Extend UrlHelper
Rails learning day 2
rails tutorial Chapter 9
[Rails] dependent settings