This time I will learn about seed.
The seed file is simply the initial data. For example, if you reset the database for some reason while coding, the data inside will of course be lost. However, if there is a user registration function, you will have to re-register as a user every time you reset. If it is a normal user, that may be fine, but if you have set the admin authority by adding a roll column to the user, grant the admin authority on the console after user registration. It will be troublesome to do it twice. So in rails, write the data you want to put in the seed file in db / seeds.rb.
This time I would like to consider a table with a structure where user has multiple notes as shown below.
seed.rb
User.create!(
email: '[email protected]',
password: 'testtest',
name: "test",
)
Category.create!(
name: 'IT',
is_valid: true,
)
Note.create!(
title: "OS",
user_id: 1,
category_id: 1,
explanation:
"OS is an abbreviation for Operation System, which is the basic software for operating apps and devices. Specifically, it conveys information entered from devices such as keyboards, mice, and touchpads to applications, and plays a central role in linking software and hardware. Personal computers and smartphones have applications that perform various tasks such as games, word processors, and spreadsheets, but they are usually developed for each OS.\r\n\r\n\r\n```\r\n example)\r\n ・ Mac OS\r\n ・ Window OS\r\Linux x\r\n```\r\n\r\n",
rate: 3,
)
I think this is the most basic and basic way of writing.
seed.rb
Note.create!(
[
{
title: "OS",
user_id: 1,
category_id: 1,
explanation:
"OS is an abbreviation for Operation System, which is the basic software for operating apps and devices. Specifically, it conveys information entered from devices such as keyboards, mice, and touchpads to applications, and plays a central role in linking software and hardware. Personal computers and smartphones have applications that perform various tasks such as games, word processors, and spreadsheets, but they are usually developed for each OS.\r\n\r\n\r\n```\r\n example)\r\n ・ Mac OS\r\n ・ Window OS\r\Linux x\r\n```\r\n\r\n",
rate: 3,
},
{
title: "UI(User interface)",
user_id: 1,
category_id: 1,
explanation:
"UI is an abbreviation for User Interface, and generally means all interfaces (contact points) between users (users) and products and services.\r\n\r\n A user is a person who mainly uses Web services (sites), applications, and software.\r\An interface literally means a point of contact or connection, and is a way of communicating or connecting between two things.\r\n\r\n The UI in a website refers to the appearance and ease of use of the site. Some people often mistakenly think that UI = appearance, but what the user sees and operates, such as the layout and images used, as well as the font of characters, the operability of menus and buttons, etc. Everything is included\r\n\r\n```\r\n ・ Screen\r\n ・ Appearance\r\n ・ Usability\r\n```",
rate: 3,
}
]
)
After writing the seed file
rails db:seed
By giving as, the contents written in seed can be reflected in the database.
This time I learned about seed. There are many other ways to write, but I will update it as soon as I learn. Also, I would appreciate it if you could teach me.
Recommended Posts