Seeding Guest Data on Rails App
After I created [Instacation](http://instacation.cssherry.com/), the site needed some guest data, particularly since most of the cool features are really only apparent after having albums and photos to play with. I handled this by populating new accounts with minimal sample data, and also creating a guest account that will automatically reset itself once a day. # Automatically create data on new accounts _Demo Album for New Accounts_
Whenever a new user is created, I run a method that will generate some sample data for them.
after_create :create_seed_album def create_seed_album demo_album = albums.create!(title: 'Demo Album', location_id: 'ChIJ60u11Ni3xokRwVg-jNgU9Yk', description: 'You can add a description of your album here!') demo_album.photos.create!([{ caption: 'Photos can have caption!', order: 0, photo_url: 'jpeg_url1.jpg', cloudinary_id: 'cloudinary_id_here', location_id: 'ChIJHa4yoSf0t1IR53T-C55D0yY' }, { caption: "Drag this photo!", order: 0, photo_url: "jpeg_url2.jpg", cloudinary_id: "cloudinary_id_here", location_id: nil }]) end
Create a guest account
Instacation's Guest Account
There's two ways to create the seed data.
Make the seed file from scratch
Make the account and data on your machine, and then export it
I went with the second option and created a pretty good user account on my computer. Then I then created a seed file using Seed Dump (Thanks Ander for the suggestion!)
Finally, to ensure that the guest account is more-or-less static, I set up a scheduled task in Heroku to run a reseed this guest account every day.
How to Seed Dump
Install seed dump (you only need to do this for the development environment.
group :development do gem 'seed_dump' # SEED DUMP! gem 'better_errors' gem 'binding_of_caller' gem 'pry-rails' gem 'quiet_assets' end
run $bundle install on the code and then create a db/seeds.rb file by running $rake db:seed:dump
I updated this seed file so that it would delete the user if it already exisits (otherwise rake db:seed will throw an error), and set the ID of my guest user, albums, and photos (otherwise, the associations will break).
# Delete user if it already exists user = User.find_by(username: 'worldtraveler') user.destroy if user ### CODE! ### # Update the User and it's albums # There's a more concise way to do this, save the user created and make the albums ontop of that user. # However, that does not work for my purpose, because I am bulk-creating a bunch of albums at one time user = User.find_by(username: 'worldtraveler') user.update(id: '1') album1 = Album.find_by(title: 'Philadelphia - The Journey Begins!') album1.update(id: '1') album2 = Album.find_by(title: 'The Great Plains - Heading Into Danger') album2.update(id: '2') album3 = Album.find_by(title: "Dakota - Racing Against Winter") album3.update(id: '3') album4 = Album.find_by(title: 'Rockies - Rockies in Sight') album4.update(id: '4')
Now, I created a task in the lib directory that would seed the guest account.
# lib/tasks/scheduler.rake desc 'This task reseeds the guest account' task seed_guest_account: :environment do Rake::Task["db:seed"].invoke end
Finally, I scheduled this task on Heroku by first installing the scheduler add-on heroku addons:add scheduler and then open the scheduler add-on for the app using heroku addons:open scheduler and add the rake task (In my case rake seed_guest_account).













