Created
April 11, 2012 19:27
-
-
Save averyvery/2361682 to your computer and use it in GitHub Desktop.
Seeding images in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
environment_seed_file = File.join(Rails.root, 'db', 'seeds', "#{Rails.env}.rb") | |
def seed_image(file_name) | |
File.open(File.join(Rails.root, "/app/assets/images/seed/#{file_name}.jpg")) | |
end | |
products = [ | |
{:name => 'foo', :description => 'lorem ipsum', :product_type => ProductType.find_by_name('Sandwiches')}, | |
{:name => 'bar', :description => 'dolerem ipsum', :product_type => ProductType.find_by_name('Soups')} | |
] | |
products.each do |attributes| | |
attributes[:image] = seed_image('product_family_or_system') | |
Product.find_or_create_by_name(attributes[:name], attributes) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also worth noting that if you want to add a WHOLE bunch of properties to everything you seed, with a few overrides, you can merge from some default attributes: