How to write Rails seed

Introduction

This time I will learn about seed.

What is 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.

Basic

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.

Multiple

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.

Finally

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

How to write Rails seed
How to write Rails
How to write Rails validation
[Rails] How to make seed
How to write Rails routing
[Rails] How to write in Japanese
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write dockerfile
How to uninstall Rails
How to write docker-compose
How to write Mockito
How to write migrationfile
Rails: How to write a rake task nicely
[Rails] How to write when making a subquery
[Rails] Various ways to write in seed files
[rails] How to post images
How to write good code
[Rails] How to use enum
Bit Tetris (how to write)
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to use rails join
How to write Junit 5 organized
[Rails] How to use validation
[Ruby] How to write blocks
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
[Ruby on Rails] How to write enum in Japanese
How to write a date comparison search in Rails
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
[Rails] Two ways to write form_with
[Rails] How to use flash messages
[rails] How to display db information
[Rails] How to prevent screen transition
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
Baseball ball count (how to write)
[Rails] How to add new pages
How to write a ternary operator
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
[Rails] How to use Active Storage
How to introduce jQuery in Rails 6
[Rails] How to implement star rating
How to return Rails API mode to Rails
How to get along with Rails
Y-shaped road tour (how to write)