Last active
September 30, 2015 21:28
-
-
Save aviflombaum/1865381 to your computer and use it in GitHub Desktop.
Association Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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