Skip to content

Instantly share code, notes, and snippets.

@bousquet
Forked from anonymous/gist:98296ce3f93d5fa53884
Last active August 29, 2015 14:03
Show Gist options
  • Save bousquet/bc7e3cf11b3949f65aae to your computer and use it in GitHub Desktop.
Save bousquet/bc7e3cf11b3949f65aae to your computer and use it in GitHub Desktop.
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