LibraryManagementSystem/db/seeds.rb

16 lines
704 B
Ruby

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
require 'csv'
library = Library.find_or_create_by(name: 'The Modern Library')
CSV.foreach(Rails.public_path.join('books_data.csv'), headers: true) do |row|
datum = row.to_h
datum['genre']= datum['genre'].to_sym
datum['sub_genre']= datum['sub_genre'].to_sym
Book.find_or_create_by(datum.merge!(library_id: library.id))
end