-
-
Save bousquet/bc7e3cf11b3949f65aae to your computer and use it in GitHub Desktop.
This file contains 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
class User < ActiveRecord::Base | |
has_many :collection_tracks | |
has_many :tracks, through: :collection_tracks | |
def collection | |
tracks | |
end | |
def add_to_collection(track) | |
collection_tracks.where(track_id: track.id).first_or_create | |
end | |
def remove_from_collection(track) | |
collection_tracks.where(track_id: track.id).try(:destroy) | |
end | |
end | |
class CollectionTracks < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :track | |
end | |
class Track < ActiveRecord::Base | |
has_many :collection_tracks | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment