aboutsummaryrefslogtreecommitdiffstats
path: root/db/seeds/seed_helpers.rb
blob: 708362a6c87d0354ef58061e9438bc796f9887bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
class ActiveRecord::Base
  def self.seed_by(key_attribute, &block)
    model = find_or_initialize_by key_attribute
    print "Seed #{name} #{key_attribute.inspect} "
    yield model

    puts "[#{(model.changed? ? 'updated' : 'no change')}]"
    model.save!

    model
  end
end