Seedie
Seedie is a Ruby gem designed to make it easy to seed your database with realistic data. Utilizing the Faker library, Seedie generates realistic data for ActiveRecord models. Currently supports only PostrgreSQL and SQLite3 databases. The gem includes a Rake task for seeding models and a Rails generator for easy setup.
Screencast
Installation
Add the following line to your application's Gemfile:
gem 'seedie'And then execute:
$ bundle installOr install it yourself as:
$ gem install seedieNext, run the install generator:
$ rails generate seedie:installThis will create a seedie.yml file in your config directory, which will include configurations for your models.
Alternatively, you can also create a blank seedie.yml file by running:
$ rails generate seedie:install --blankThis will generate a blank seedie.yml config file for you that you can now customize according to your needs.
Usage
Generating blank seedie.yml
If you want to create a blank seedie.yml file, use the --blank option:
$ rails generate seedie:install --blankThis will generate a blank seedie.yml config file for you that you can now customize according to your needs.
Excluding Models
If you want to exclude certain models while generating the seedie.yml, use the --excluded_models option:
$ rails generate seedie:install --excluded_models User Admin PostNOTE: Some models may not be excluded because of their dependencies. For example, if you have a model Post that belongs to a model User, then the User model will not be excluded even if you specify it in the --excluded_models option.
You'll get a warning in your console if any models are not excluded:
WARNING: User has dependencies with other models and cannot be excluded.Including only few Models
If you want to include only few particular models while generating the seedie.yml, use the --include_only_models option:
$ rails generate seedie:install --include_only_models Post CommentNOTE: Some models may be a dependency for the required models and will need to be included for successful seeding. For example, if you have a model Post that belongs to a model User, then the User model will need to be included even if you didn't specify it in the --include_only_models option.
You'll get a warning in your console if any other models are included:
WARNING: User is a dependency of included models and needs to be included.Add Custom Attributes inside seedie.rb initializer
While generating the seedie.yml file, there are default values.
For example, for every string field, the default value is {{Faker::Lorem.word}}.
This works fine for most attributes, but for some there are validations which breaks the seeding.
Take email as an example, the default value {{Faker::Lorem.word}} will not be a valid email.
By default, when we generate the seedie.yml file, we add a seedie.rb initializer file in the config/initializers directory.
Seedie.configure do |config|
# config.default_count = 10
config.custom_attributes[:email] = "{{Faker::Internet.unique.email}}"
# Add more custom attributes here
endThis ensures that the email field is seeded with a valid email address.
You can add more custom attributes in the seedie.rb initializer file.
Seeding Models
To seed your models, run the following Rake task:
$ rake seedie:seedThis will use the configurations specified in seedie.yml to seed your models.
The seedie.yml file has entries for each model in your application, and you can customize the configuration for each one.
Here's an example of a more advanced configuration in seedie.yml:
default_count: 5
models:
user:
attributes:
name: "name {{index}}"
email: "{{Faker::Internet.email}}"
address: "{{Faker::Address.street_address}}"
disabled_fields: [nickname password password_digest]
post:
count: 2
attributes:
title: "title {{index}}"
category:
values: [tech, sports, politics, entertainment]
options:
pick_strategy: random # or sequential
associations:
has_many:
comments: 4
belongs_to:
user: random # or new
has_one:
post_metadatum:
attributes:
seo_text: "{{Faker::Lorem.paragraph}}"
has_and_belongs_to_many:
tags:
count: 3
attributes:
name: "{{Faker::Lorem.word}}"
disabled_fields: []
comment:
attributes:
title: "title {{index}}"
associations:
belongs_to:
post:
attributes:
title: "Comment Post {{index}}"
In this file:
-
default_countspecifies the number of records to be generated for each model when no specific count is provided in the model's configuration. -
modelsis a hash that contains a configuration for each model that should be seeded. -
attributesis a hash that maps field names to the values that should be used. If attributes are not defined, Seedie will use Faker to generate a value for the field.- The special
{{index}}placeholder will be replaced by the index of the current record being created, starting from 1. This allows you to have unique values for each record. - Additionally, we can use placeholders like
{{Faker::Internet.email}}to generate dynamic and unique data for each record using Faker. - We can also specify an array of values that can be picked from randomly or sequentially using the
valuesandpick_strategyoptions.
- The special
-
disabled_fieldsis an array of fields that should not be automatically filled by Seedie. -
associationsspecify how associated models should be generated. Here,has_many,belongs_to,has_oneandhas_and_belongs_to_manyare supported. - The specified number for
has_manyrepresents the number of associated records to create. - For
belongs_to, the valuerandommeans that a random existing record will be associated. If there is a unique index associated, thenuniquewill be set or elserandomis the default. - If attributes are specified under an association, those attributes will be used when creating the associated record(s)
- When using associations, it's important to define the models in the correct order in the
seedie.ymlfile. Associated models should be defined before the models that reference them.
Development
After checking out the repo, run bin/setup to install dependencies.
Then, run bundle exec rspec to run the tests.
By default, the tests will supress output of the seeds progress.
Use DEBUG_OUTPUT=true bundle exec rspec to see the output of the seeds.
You can also run bin/console for an interactive prompt that will allow you to experiment.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/keshavbiswa/seedie.
License
The gem is available as open source under the terms of the MIT License.