Adding a Custom Seed File

Adding a custom seed file

Start by creating a separate directory to hold your custom seeds – this example uses db/seeds. Then, create a custom task by adding a rakefile to your lib/tasks directory:

# lib/tasks/custom_seed.rake
namespace :db do
namespace :seed do
Dir[Rails.root.join('db', 'seeds', '*.rb')].each do |filename|
task_name = File.basename(filename, '.rb')
desc "Seed " + task_name + ", based on the file with the same name in `db/seeds/*.rb`"
task task_name.to_sym => :environment do
load(filename) if File.exist?(filename)
end
end
end
end

This rakefile accepts the name of a seed file in the db/seeds directory (excluding the .rb extension), then runs it as it would run seeds.rb. You can execute the rake task by issuing the following from the command line:

rake db:seed:file_name # Name of the file EXCLUDING the .rb extension 

Update: Now it should also list the seed tasks when running rake --tasks or rake -T.

Is there a way to db:seed only one table in Rails?

With below custom rake file, you can have multiple seed files in db/seeds/ folder.

# lib/tasks/custom_seed.rake
# lib/tasks/custom_seed.rake
namespace :db do
namespace :seed do

Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].each do |filename|
task_name = File.basename(filename, '.rb').intern

task task_name => :environment do
load(filename)
end
end

task :all => :environment do
Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].sort.each do |filename|
load(filename)
end
end

end
end

Then, in order to run specific seed file, you can just run

rake db:seed:seed_file_name

To run all the seeds file with order in that db/seeds folder, run below command

rake db:seed:all

Ruby On Rails: way to create different seeds file for environments

You can have a rake task behave differently based on the current environment, and you can change the environment a task runs in by passing RAILS_ENV=production to the command. Using these two together you could produce something like so:

Create the following files with your environment specific seeds:

db/seeds/development.rb
db/seeds/test.rb
db/seeds/production.rb

Place this line in your base seeds file to run the desired file

load(Rails.root.join( 'db', 'seeds', "#{Rails.env.downcase}.rb"))

Call the seeds task:

rake db:seed RAILS_ENV=production 

Create seed file from data already in the database

There is a gem called seed_dump, which will do exactly what you want:

  • https://github.com/rroblak/seed_dump
  • http://rubygems.org/gems/seed_dump

Adding image to seed database in rails 5.2 active storage

You are passing a directory not a file in this code:

File.open('/Users/bradley/')

If your image path is: /Users/bradley/Dart.png. Then you need to change your code:

File.open('/Users/bradley/Dart.png')

However I would not recommend to use absolute paths in your project even if it was only for seeds. You can add the image to your rails project and use relative path to your project.

How to add new seed data to existing rails database

Structure your seed.rb file to allow ongoing creation and updating of data. You are not limited to running a seed file only once and if you think it's only used for initial deployment you will miss out on the flexibility it can offer in setting reference data.

A seed file is just ruby so you can do things like:

user = User.find_or_initialize_by(email: 'bob@example.com')
user.name = 'Bob'
user.password = 'secret'
user.role = 'manager'
user.save!

This will create new data if it doesn't exist or update the data if it finds some.

If you structure your seed file correctly you can also create and update dependent objects.

I recommend using the bang save to ensure that exceptions are raised in the event that an object cannot be saved. This is the easiest method of debugging the seed.

I use the seedbank gem to provide more structure to my seed data, including setting data per environment, dependent seeds and more.

I don't recommend using migrations for seed data. There is a lack of flexibility (how do you target seed data to just one environment for instance) and no real way to build up a reusable set of data that can be run at any time to refresh a particular environment. You would also have a set of migrations which have no reference to your schema and you would have to create new migrations every time you wanted to generate new or vary current data.

How do I seed my database with only part of my seed code?

You can pass an option while running rake db:seed like following:

rake db:seed users=yes

And, then in your code, you can access it through the following way:

20.times do
Employee.create!(name: "Bob",
email: Faker::Internet.email)
end

if ENV["users"]
20.times do
User.create!(name: "Hank",
password: "foobar")
end
end

How does one make a custom seed Rakefile

Quick and dirty:

YAML::load(file).each do |topic, item_types|
Topic.create! :name => topic, :items => item_types.map { |type, names|
names.split(' ').map { |name|
Item.new :type => type, :name => name
}
}.flatten
end


Related Topics



Leave a reply



Submit