[For beginners] ○○. △△ in Ruby (ActiveRecord method, instance method, data acquisition)

Background of writing this article

While learning Ruby, I saw a lot of code with patterns of ○○. △△ (example: User.new). I wondered if they were used differently, and found three patterns of usage. So, this time I have summarized the patterns of ○○. △△.

Target audience

A fledgling engineer learning Ruby

Implementation environment

Ruby 2.6.5 Rails 6.0.3.3

There were three patterns

① Model (class) name.ActiveRecord method name

Used for table operations on models. In the example below, it is the part of "Tweet.all", "Tweet.new", "Tweet.find (params [: id])".

class TweetsController < ApplicationController
  def index
 Get all data of #Tweet model (Tweets table)
    @tweet = Tweet.all
  end


  def new
 Instantiation of #Tweet model
    @tweet = Tweet.new
  end

  def edit
 Get one data (1 record) with #Tweet model (Tweets table)
    @tweet = Tweet.find(params[:id])
  end
end

See this article for the ActiveRecord method. It was really easy to understand. https://qiita.com/ryokky59/items/a1d0b4e86bacbd7ef6e8

(2) Instance (variable) name. Instance method name

Use the method by attaching it to the instance. Instance methods cannot be used without creating an instance. In the example below, it is the "number.calc (1,2)" part.

class Number
  def calc(a, b)
    puts a + b
  end
end

number = Number.new()
number.calc(1,2)

③ Variable name.Attribute name

It is used to get the attribute value stored in the variable. In the example below, it is the "user.nickname" part. Gets the data in the nickname column of the User table.

class UsersController < ApplicationController
  def show
 Get one data (1 record) with #User model (Users table) and assign it to variable user
    user = User.find(params[:id])
 Get the information (attribute value) of the nickname column of the #variable user and assign it to the instance variable @nickname.
    @nickname = user.nickname
  end
end

That's it. I'm glad if you can use it as a reference.

Recommended Posts

[For beginners] ○○. △△ in Ruby (ActiveRecord method, instance method, data acquisition)
abbreviation for ruby method
Scraping for beginners (Ruby)
Data acquisition method memo when there is HashMap in HashMap
Java for beginners, data hiding
Rock-paper-scissors game for beginners in Java
[For beginners] Run Selenium in Java
String output method memo in Ruby
Recommended learning method for programming beginners
[Ruby] undefined method `dark?'occurs in rqr_code
How to get date data in Ruby
Acquisition of article information in ruby ​​scraping
[For programming beginners] What is a method?
[Ruby on Rails] About bundler (for beginners)
[Ruby] How to use slice for beginners
Tips for gRPC error handling in Ruby
Explanation of Ruby on rails for beginners ①
Beginners create portfolio in Ruby on Rails
[For beginners] How to debug in Eclipse
Java-database connection Java-MySQL connection (2): JDBC driver acquisition method (for MySQL) and data connection preparation / September 2017
[Order method] Set the order of data in Rails
[For beginners] I tried using DBUnit in Eclipse
[For beginners] I tried using JUnit 5 in Eclipse
(Ruby on Rails6) Creating data in a table
[For Ruby beginners] Ruby engineer certification exam Silver/Gold exam notes
Processing for reading / refreshing / updating data in TableView
About regular expressions used in ruby sub method
How to implement Pagination in GraphQL (for ruby)