mcfearsome (owner)

Fork Of

gist: 156915 by anonymous

Revisions

  • 8a838e Mon Jul 27 21:08:34 -0700 2009
gist: 156922 Download_button fork
public
Public Clone URL: git://gist.github.com/156922.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# == Schema Information
#
# Table name: tracks
#
# id :integer(4) not null, primary key
# title :string(128)
# album :string(128)
# artist :string(128)
# bitrate :integer(4)
# playcount :integer(4)
# key :string(12)
# audio_id :string(512)
# bpm :integer(4)
# ranking :integer(4)
# filename :string(256)
# created_at :datetime
# updated_at :datetime
#
 
class Track < ActiveRecord::Base
  has_many :pairs
  has_many :tracks,
    :through => :pairs,
    :after_add => :create_reverse_association,
    :after_remove => :remove_reverse_association
  has_many :partners,
    :through => :pairs,
    :after_add => :create_reverse_association,
    :after_remove => :remove_reverse_association
  has_many :master_pairs,
    :through => :pairs
    
    
  def create_reverse_association(partner_track)
    unless partner_track.partners.include?(self)
      partner_track.partners << self
    end
  end
  def remove_reverse_association(partner_track)
    partner_track.partners.delete(self) if partner_track.partners.include?(self)
  end
end