Skip to content

Instantly share code, notes, and snippets.

@wikjez
Forked from averyvery/seeds.rb
Created May 19, 2016 00:26
Show Gist options
  • Save wikjez/ebd28095d72a3bc258ab4373d9e44467 to your computer and use it in GitHub Desktop.
Save wikjez/ebd28095d72a3bc258ab4373d9e44467 to your computer and use it in GitHub Desktop.
Seeding images in Rails
# 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