Module: SeedFu::ActiveRecordExtension

Included in:
ActiveRecord::Base
Defined in:
lib/seed-fu/active_record_extension.rb

Instance Method Summary collapse

Instance Method Details

#seed(*args, &block) ⇒ Object

Load some seed data. There are two ways to do this.

Verbose syntax


This will seed a single record. The ‘:id` parameter ensures that if a record already exists in the database with the same id, then it will be updated with the name and age, rather than created from scratch.

Person.seed(:id) do |s|
  s.id   = 1
  s.name = "Jon"
  s.age  = 21
end

Note that ‘:id` is the default attribute used to identify a seed, so it need not be specified.

Terse syntax


This is a more succinct way to load multiple records. Note that both ‘:x` and `:y` are being used to identify a seed here.

Point.seed(:x, :y,
  { :x => 3, :y => 10, :name => "Home" },
  { :x => 5, :y => 9,  :name => "Office" }
)


31
32
33
# File 'lib/seed-fu/active_record_extension.rb', line 31

def seed(*args, &block)
  SeedFu::Seeder.new(self, *parse_seed_fu_args(args, block)).seed
end

#seed_once(*args, &block) ⇒ Object

Has the same syntax as #seed, but if a record already exists with the same values for constraining attributes, it will not be updated.

Examples:

Person.seed(:id, :id => 1, :name => "Jon") # => Record created
Person.seed(:id, :id => 1, :name => "Bob") # => Name changed
Person.seed(:id, :id => 1, :name => "Harry") # => Name *not* changed


42
43
44
45
# File 'lib/seed-fu/active_record_extension.rb', line 42

def seed_once(*args, &block)
  constraints, data = parse_seed_fu_args(args, block)
  SeedFu::Seeder.new(self, constraints, data, :insert_only => true).seed
end