Skip to content

Instantly share code, notes, and snippets.

@seldomatt
Forked from aviflombaum/seed.rake
Created July 19, 2012 18:59
Show Gist options
  • Save seldomatt/3146007 to your computer and use it in GitHub Desktop.
Save seldomatt/3146007 to your computer and use it in GitHub Desktop.
seed rake file homework (associations)
desc "Create a bunch of seed data for artists and songs"
task :seed_artists_and_songs => [:environment, :clear_artists_and_songs] do
# Build Song Off Artist
# Given a Song called R.E.S.P.E.C.T
# build the Aretha Franklin Artist
s = Song.create(:name => "R.E.S.P.E.C.T")
s.build_artist(:name => "Aretha Franklin")
s.save
# Build a Song for that Artist
# build song Natural Woman
aretha = Artist.first
natural_woman = Song.create(name: "Natural Woman")
a.songs << x
# Manually Create Artist Kanye West
k = Artist.create(:name => "Kanye West")
# Push a Song Onto an Artist
# push a song onto Kanye West
m = Song.create(:name => "Monster")
k.songs << m
# Create Michael Jackson Artist
michael = Artist.create(:name => "Michael Jackson")
# Make 3 Michael Jackson Songs
billie_jean = Song.create(name: => "Billie Jean")
man_in_the_mirror = Song.create(name: => "Man in the Mirror")
thriller = Song.create(name: => "Thriller")
# push all songs by Michael Jackson
michael.songs << [billie_jean, man_in_the_mirror, thriller]
# Delete one michael song off of michael
michael.songs.delete(man_in_the_mirror)
# Remove a Song from Artist
end
task :clear_artists_and_songs => [:environment] do
puts "Deleting all Artists and Songs...."
Artist.delete_all
Song.delete_all
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment