Depot¶ ↑
Depot is a simple and more maintainable way to define seed data in Rails.
Installation¶ ↑
Add this line to your application’s Gemfile:
gem "depot", "~> 0.2", require: false
Usage¶ ↑
You’ve probably did that in the old days of your db/seeds.rb
life.
# This file should contain all the record creation needed to seed the database with its default values. # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). # # Examples: # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first)
If there were a better way to populate your database it would be like this:
require "depot" Depot.construct do cities do create name: 'Chicago', as: :chicago create name: 'Copenhagen' end # If you prefer you can use the block syntax too. mayors do create do |m| m.name = 'Emanuel' m.city = chicago end end end
Pretty simple, huh?
License¶ ↑
(The MIT License)
Copyright © 2012 Rainer Borene
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, an d/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.