Ruby methods often used in Rails

Introduction

It's been about 4 months since I changed my job to a web contract development company that mainly handles Ruby on Rails. Even though I am a beginner, I am having a hard time assigning to the project and having a good time every day. I will summarize the Ruby methods that I think I will not use often during development!

Fix

--20200531 Corrected based on the comments received.

Method list

if A control structure that realizes conditional branching that does not require explanation. Evaluate with true or false and branch the process to be executed.

if conditional expression
true processing for if
elsif conditional expression
True processing for elsif
else
Handling of false
end

each A method to get the elements of an array in an iterative form. Rails uses it to get DB records in order.

[1, 2, 3].each do |i|
  p i
end
output
1
2
3

map This is also similar to the each method, but It is used when specifying columns when retrieving DB records.

For example, if you have the following table

Users table
id |name        | age  | sex  | 
1  |Test Ichiro| 20  |Man|
2  |Test Hanako| 18  |woman|
3  |Test Jiro| 15  |Man|

You can specify the column and retrieve it in this way.

Users.all.map(&:age)

output
[20, 18, 15]

Get the id etc. of the table by specifying the column, This is useful for browsing the associated table. It is also often used in combination with the Where method.

where It is used when searching by specifying columns and data when retrieving records from the DB. If a table similar to map exists, you can get it as follows.

Users.where(age: 15)

output
id |name        | age  | sex  |
3  |Test Jiro| 15  |Man|

first Gets the first record in a DB that has multiple records. If a table similar to map exists, you can get it as follows.

Users.first

output
id |name        | age  | sex  | 
1  |Test Ichiro| 20  |Man|

present?,blank?,nil? present ?: true if the value exists, false if it does not exist. When evaluating an array, if it is [], it will be nil and false. blank ?: True if there is no value. nil ?: True if the value of the variable is nil or no value.

Ternary operator

It is often used when there is only one conditional branch by if. Conditional branching can be done with one line, and I personally think it is smart.

Conditional expression?true processing:Handling of false

count It counts DB records and uses them for pagination. If a table similar to map exists, you can get it as follows.

Users.count

output
3

References

Ruby 2.7.0 Reference Manual [Introduction to Ruby] Easy-to-understand explanation of how to use authenticity judgment present?

Recommended Posts

Ruby methods often used in Rails
Gem often used in Rails
About methods often used in devise
Matcher often used in RSpec
Test API often used in AssertJ
Ruby on Rails Japanese-English support i18n
Frequently used methods in Active Record
Commands often used in MySQL operations
Ruby on Rails in Visual Studio Codespaces
Methods that I found useful in Ruby
Beginners create portfolio in Ruby on Rails
Class in Ruby
Group_by in Rails
Heavy in Ruby! ??
Ruby Learning # 15 Methods
A collection of methods often used when manipulating time with TimeWithZone of Rails
About Ruby methods
Ruby array methods that can be used with Rails (other than each)
List of methods used in PAIZA D rank
Recommendation of Service class in Ruby on Rails
Rails new in Ruby on Rails ~ Memorandum until deployment 2
Rails new in Ruby on Rails ~ Memorandum until deployment 1
(Ruby on Rails6) Creating data in a table
[Ruby] Methods that can be used with strings
[Ruby on Rails] How to install Bootstrap in Rails
About regular expressions used in ruby sub method
Summary of frequently used commands in Rails and Docker
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Chapter 4 Rails Flavored Ruby
Read mp3 tag (ID3v1) in Ruby (no library used)
Model association in Rails
Adding columns in Rails
About eval in Ruby
Disable turbolinks in Rails
[Updated from time to time] Ruby on Rails Convenient methods
CSRF measures in Rails
About Ruby instance methods
[Ruby On Rails] How to reset DB in Heroku
[Ruby / Rails] Set a unique (unique) value in the class
Ruby on Rails Elementary
Ruby on Rails basics
[Ruby on Rails] Post image preview function in refile
^, $ in Rails regular expression
Personal summary of the guys often used in JUnit 4
Use images in Rails
Ruby variables and methods
Output triangle in Ruby
Understand migration in rails
[Ruby] methods, instance methods, etc ...
Ruby On Rails Association
Variable type in ruby
Ruby, Rails finally installed
Split routes.rb in Rails6
Fast popcount in Ruby
In Ruby, methods with? Do not always return true/false.
Implement markdown in Rails
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
Definitely useful! Debug code for development in Ruby on Rails
[Ruby on Rails] Quickly display the page title in the browser
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)