class Planter::Seeder

Class that seeders should inherit from. Seeders should be in db/seeds, and named TABLE_seeder.rb, where TABLE is the name of the table being seeded (for example, users_seeder.rb). If your seeder is named differently than the table, you’ll need to specify the table with the table option. The seeder’s class name should be the same as the file name, but camelized. So, UsersSeeder. The directory where the seeder files are located can be changed via an initializer.

To generate a seeder with a specific style, pass +–seeding-method=csv+, +–seeding-method=data-array+, or +–seeding-method=custom+ to +rails generate planter:seeder+. When the csv method is used, the generator also creates a CSV file with headers pulled from the table.

The most basic way to seed is to have a CSV file with the same name as the table in db/seed_files/. So, users.csv. This CSV should have the table’s column names as headers. To seed using this method, your class should look like the following. Note that :csv_name and :table are only required if your seeder or CSV are named differently than the table being seeded. The directory where the seed files are kept can be changed via an initializer.

# db/seeds/users_seeder.rb
require 'planter'
class UsersSeeder < Planter::Seeder
  seeding_method :csv, csv_name: :users, table: :users
end

Another way to seed is to create records from a data array. To do this, your class must implement a data attribute or method, which is an array of hashes. Note that this class already provides the attr_reader for this attribute, so the most you have to do is create instance variables in your constructor. If you want your data to be different for each new record (via Faker, +Array#sample+, etc.), you’ll probably want to supply a method called data that returns an array of new data each time.

require 'planter'
class UsersSeeder < Planter::Seeder
  seeding_method :data_array
  def data
    [{foo: 'bar', baz: 'bar'}]
  end
end

In both of the above methods, you can specify a parent association, which is interpreted by the configured adapter. With the default Active Record adapter, parent is the belongs_to association name on a model-backed table. When specified, records will be created for each record in the parent table. Note that nothing is automatically done to prevent any validation errors; you must do this on your own, most likely using Faker or a similar library.

require 'planter'
class UsersSeeder < Planter::Seeder
  seeding_method :data_array, parent: :person
  def data
    [{foo: 'bar', baz: 'bar'}]
  end
end

You can also set number_of_records to determine how many times each record in the data array will be created. The default is 1. Note that if this attribute is set alongside parent, number_of_records will be how many records will be created for each record in the parent table.

require 'planter'
class UsersSeeder < Planter::Seeder
  seeding_method :data_array, number_of_records: 5
  def data
    [{foo: 'bar', baz: 'bar'}]
  end
end

By default, all fields are used to look up the record. If it already exists, it is not re-created. If you have specific fields that a record should be looked up by, you can pass the unique_columns option. This will attempt to look up the record by those fields only, and if one doesn’t exist, one will be created with the rest of the attributes. An example of when this would be useful is with Devise; you can’t pass password in the create method, so specifying unique_columns on everything except password allows it to be passed as an attribute to the first_or_create call.

require 'planter'
class UsersSeeder < Planter::Seeder
  seeding_method :data_array, unique_columns: %i[username email]
  def data
    [{username: 'foo', email: 'bar', password: 'Example'}]
  end
end

If you need to seed a different way, put your own custom seed method in your seeder class and do whatever needs to be done.