Skip to content

Instantly share code, notes, and snippets.

@aviflombaum
Last active September 30, 2015 21:28
Show Gist options
  • Select an option

  • Save aviflombaum/1865381 to your computer and use it in GitHub Desktop.

Select an option

Save aviflombaum/1865381 to your computer and use it in GitHub Desktop.
Association Examples
# Assign the Artist to the Song
artist = Artist.create()
song = Song.create()
song.artist = artist
song.save
artist.reload
artist.songs
# Build the Song from the Artist
artist = Artist.create()
artist.songs.build
artist.save
# Build the Artist from the Song
song = Song.create()
song.build_artist
song.save
# Add the Song to the Artist
artist = Artist.create()
song = Song.create()
artist.songs << song
# artist.songs = [song]
# artist.save
# Assigning Songs to a Mixtape
# Directly through
mixtape = Mixtape.create
song = Song.create
song.mixtapes << mixtape
mixtape = Mixtape.create
song = Song.create
mixtape.songs << song
mixtape = Mixtape.create
song = mixtape.songs.build
mixtape.save
song = Song.create
mixtape = song.mixtapes.build
song.save
# Via the MixtapeSong
mixtape = Mixtape.create
song = Song.create
mixtape.mixtape_songs.build(:song => song)
mixtape.save
mixtape = Mixtape.create
song = Song.create
song.mixtape_songs.build(:mixtape => mixtape)
song.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment