[Rails] Reflection to db using seeds.rb

Purpose of this post

・ Minutes about Rails.

Teaching materials used for learning

I have summarized it with reference to the following two teaching materials of Udemy. ・ ["Introduction to Ruby on Rails for the first time-Learn Ruby and Rails from the basics and publish your web application online"](https://www.udemy.com/course/the-ultimate-ruby-on-rails-bootcamp/) ・ ["Rails engineer training course taught by full stack engineers"](https://www.udemy.com/course/rails-kj/)

○ About seeds.rb

-Used when inputting initial data before implementing new post view -Convenient when implementing a large amount of data compared to rails db console ・ (Basically, it is generally created from rails console)

○ How to write seeds.rb

-Write the code in the project name /db/seeds.erb ・ This time, the user data of the repeating pattern of numbers is reflected in 50 db.

qiita.rb


if Rails.env == 'development'
  (1..50).each do |i|
model name.create(name: "user#{i}", title: "title#{i}", body:"Text#{i}")
  end
end

[Explanation] Rails.env == 'development' -Determine whether the rails execution environment is in development mode ・ There are three types of env: "development", "test", and "production".

model name.cretate (column name: value, column name: value) · Code that stores the value in the database ・ Model name should be described in the singular system.

[About create method] It is possible to describe vertically by doing as follows

qiita.rb


model name.create([
  { name: 'Value 1' },
  { name: 'Value 2' },
  { name: 'Value 3' },
  { name: 'Value 4' },
  { name: 'Value 5' },
  { name: 'Value 6' }
])
end

[Explanation] -Write in hash format in the array ・ Multiple data can be saved together

○ Execution of seeds.rb

rails db:seed

-Code to reflect the information described in seeds.rb in db


Recommended Posts

[Rails] Reflection to db using seeds.rb
[rails] How to display db information
Rails DB PostgreSQL changed to MySQL
[Rails] How to upload images using Carrierwave
How to implement image posting using rails
[Rails] How to handle data using enum
rails db: 〇〇 Summary
[Rails] How to create a graph using lazy_high_charts
[Java] Convert DB code to code value using enum
[Rails] How to upload multiple images using Carrierwave
[Rails MySQL] How to reset DB on heroku
How to write Rails
Introducing CircleCI to Rails
Introducing Bootstrap to Rails 5
Introducing Bootstrap to Rails !!
Introduce Vue.js to Rails
DB access using Exposed
rails db: migrate failed!
[Ruby On Rails] How to reset DB in Heroku
[Rails] rails db command summary
How to uninstall Rails
I want to call the main method using reflection
[Rails] Create sitemap using sitemap_generator and deploy to GAE
[Rails 6] Add images to seed files (using Active Storage)
Rails learning How to implement search function using ActiveModel
[Rails] How to install a decorator using gem draper
Try to implement tagging function using rails and js
How to set environment variables when using Payjp with Rails
Add an icon to the header link using Rails fontawesome
What I was addicted to while using rspec on rails
Prevent operations! How to securely update Rails manually using transactions
What to check when rails db: migration does not pass
I tried using Hotwire to make Rails 6.1 scaffold a SPA
# What to do if you accidentally do rails db: migrate: drop
How to specify db when creating an app with rails
How to deploy jQuery in your Rails app using Webpacker
[Rails] I want to display "XX minutes ago" using created_at!
[Rails] How to delete images uploaded by carrierwave (using devise)
[Rails] Integration test using Capybara (from introduction to simple test execution)
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
[Rails] I tried to implement "Like function" using rails and js
8 things to insert into DB using Spring Boot and JPA
Things to keep in mind when using Sidekiq with Rails
[rails] How to post images
rails test db only drop
Search function using [rails] ransack
Try using view_component with rails
SNS authentication using Rails google
[Rails] Save images using carrierwave
[Rails] How to use enum
[Rails] How to install devise
[Rails] How to use enum
DB programming using EclipseLink part1
Japaneseize using i18n with Rails
[Rails] Japanese localization using rails-i18n
How to read rails routes
Error in rails db: migrate
How to use rails join
[Rails] Test code using Rspec
Organize Rails routing using draw
Item 65: Prefer interfaces to reflection