fixin.me/db/seeds.rb
cryptogopher 3788f1a749 Fix db:seed:export task
* replace variable names with Hash to avoid invalid Ruby identifiers
* export all values as single-quoted to avoid string interpolation and
  treating BigDecimal numbers as Float
* #truncate table instead of #delete_all to avoid foreing_key
  constraints errors

Closes #56
2025-01-05 21:05:47 +01:00

26 lines
933 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).
# Seeding process should be idempotent.
User.transaction do
break if User.find_by status: :admin
User.create! email: Rails.configuration.admin, password: 'admin', status: :admin do |user|
user.skip_confirmation!
print "Creating #{user.status} account '#{user.email}' with password '#{user.password}'..."
end
puts "done."
rescue ActiveRecord::RecordInvalid => exception
puts "failed. #{exception.message}"
end
# Formulas will be deleted as dependent on Quantities
#[Source, Quantity, Unit].each { |model| model.defaults.delete_all }
# To clear contents of the table, use #truncate instead of #delete_all. This
# avoids foreign_key constraints errors.
require_relative 'seeds/units.rb'