require "open-uri" rand(5..10).times do |seed| user = User.create!( email: Faker::Internet.email, password: Faker::Internet.password, username: Faker::Internet.username ) puts user.email def add_photo(url, resource, attachment_type) file = URI.open(url) filename = File.basename(URI.parse(url).path) resource.send(attachment_type).attach(io: file, filename: filename) resource.save! end # Avatars url = Faker::Image.unsplash(height: 100, width: 100, keyword: "selfie") add_photo(url, user, "avatar") sleep 2 # Communities rand(5..10).times do community = Community.create!( name: Faker::Hipster.word, user: user ) puts community.name add_photo(url, community, "avatar") sleep 2 # Posts rand(5..10).times do chuckie = rand(5..10).times.reduce("") do |facts| "#{ facts } #{ Faker::ChuckNorris.fact }" end post = Post.create!( title: Faker::TvShows::AquaTeenHungerForce.quote, body: chuckie, community: Community.all.sample, user: User.all.sample ) puts post.title rand(1..5).times do url = Faker::Image.unsplash(height: 640, width: 480, keyword: Faker::Hipster.word) add_photo(url, post, "images") sleep 2 end end end end