The author of this article is a beginner just starting to learn programming. I would appreciate it if you could point out any mistakes.
This article is a personal memo of what I learned by reading the Ruby on Rails 6 Practical Guide. It seems to be difficult to read because it is excerpted and picked up. Excuse me. This book also has a sequel Extension, and both books have been studied at the stage of writing the article. I will write an article for review as well. I will skip cp1 and cp2 of the function extension because they explain the environment construction and the code of the main part.
Previous article Ruby on Rails6 Practical Guide cp4 ~ cp6 [Memo] Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo] Ruby on Rails6 Practical Guide cp10 ~ cp12 [Memo] Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo] Ruby on Rails6 Practical Guide cp16 ~ cp18 [Memo]
The add_index method generates a name by default as follows:
However, there is a limit to the name of the index, so if you combine a large number of columns as a composite index, you may exceed the limit. In such cases, you need to specify the index name with the name option.
add_index :customers, [ :birth_year, :family_name_kana, :given_name_kana ],
name: "index_customers_on_birth_year_and_furigana"
If a composite index is set for columns X, Y, and Z, this composite index will be used for searches for column X alone, searches that combine columns X and Y, and searches that combine three columns. However, this composite index is not used for a search for column Y alone, a search for column Z alone, or a search for a combination of columns Y and Z. So, to optimize the search for all combinations, you need to set the index as below.
Both methods return nil when the receiver is nil, but the behavior is slightly different when the receiver is other than nil. If you try to call a method that is not defined in the receiver with &., NoMethodError will occur, but if it is try, nil will be returned.
user&.name
user.try(:name)
In the above example, if there is no name in the instance method of user, NoMethodError will occur only when &. Is used.
attr_accessor :name, :gender, :birthday
def search
rel = User
rel = rel.where(name: name) if name.present?
rel = rel.where(gender: gender) if gender.present?
rel = rel.where(birthday: birthday) if birthday.present?
rel = rel.order(:name)
end
The code above is a form object. Defines a search object that returns search results. Since where and order return a Relation object, you can store various search conditions in the Relation object as in the code above.
rel = rel.joins(:articles)
rel = rel.where("articles.title" => title) if title.present?
The joins method does a table join </ strong>. Table joins allow you to filter records based on column values in other tables. The argument of the joins method is the name of the association. This method also returns a Relation object. When you join a table, you can search for other tables like the code in the second line.
rel = rel.distinct
You can remove duplicates from the search results by calling the distinct method.
cookies
cookies.signed[:user_id] = user.id
Calling the signed method makes the cookie value unreadable and immutable.
cookies.permanent.signed[:user_id] = user.id
The permanent method sets the cookie expiration after 20 years.
cookies.delete(:user_id)
You can erase the ID recorded in the cookie with the above code.
expect(response.cookies).to have_key("customer_id")
expect(response.cookies["customer_id"]).to match(/[0-9a-f]{40}\z/)
The first line looks to see if the cookie has a key called "customer_id". The second line looks to see if the cookie value is disabled. Non-viewable cookies are characterized by having a 40-digit hexadecimal number at the end, so we are investigating this using regular expressions.
validates :octet1, numericality: { only_integer: true },
inclusion: { in: 0..255 }
numericality verifies if the value is only a number. If you specify true for only_integer, only integers will be matched. With inclusion alone, a string like "XYZ" will be changed to the integer 0, so no error will occur. numericality will result in an error because validation is performed on the value before conversion.
The Rails Guide (https://railsguides.jp/active_record_validations.html#numericality) has more details.
Many-to-many associations can be divided into a combination of two one-to-many associations using a linked table.
number_with_delimiter(100000)
100,000 will be returned.
When a customer and a program have a many-to-many relationship
program.cusotmers.count
You can count the number of participants in the program as follows. However, the performance will be worse because complicated queries using JOIN will be issued.
It is the same even if you count the link table applications (entry), so you can also write as below.
program.entries.count
Counting the number of linked tables will prevent complex queries from being issued and improve performance.
With the above code, the number of applicants is counted for each program, so there is an "N + 1 problem". The code below will allow you to get the number of applicants for each program with a single query.
Program.joins(:entries)
.select("programs.*, COUNT(entries.id) AS number_of_applicants")
.group("programs.id")
Specify the name of the association in the joins argument. It is not a table name. In the argument of select method, specify the column to get the value from the table. The SQL function COUNT returns the number of records for which the value of the column specified in the argument is not NULL. Since AS names the value on the left side, you can get the number of records in the entries table as a column called number_of_applicants. The group method divides the records into groups based on the column specified in the argument. If you specify an aggregate function such as COUNT in select, you must call it in principle. By dividing into groups, you can get the number of entries for each program_id.
program[:number_of_applicants]
You can refer to it as above.
When you join tables with the joins method normally, records that are not referenced at all from the joined table will be excluded from the search results. In other words, only programs for which one or more applications have been submitted will be searched.
left_joins(:entries)
If you rewrite joins with left_joins, unreferenced records will remain.
We will add the URLs of the following articles one by one.
Ruby on Rails6 Practical Guide [Extensions] cp7 ~ cp9 [Memo] Ruby on Rails6 Practical Guide [Extensions] cp10 ~ cp12 [Memo]
Recommended Posts