Skip to content

Instantly share code, notes, and snippets.

Created February 25, 2013 05:08
Show Gist options
  • Save anonymous/5027875 to your computer and use it in GitHub Desktop.
Save anonymous/5027875 to your computer and use it in GitHub Desktop.
Track.last.authors => works
Track.last.original_author => doesn't work
User.last.creations => works
User.last.original_creations => doesn't work.
I'm still a beginner and not entirely sure how the relationships work, and which words need to match. For example, does "author" need to match in "belongs_to :author" from Track_Author with "has_many :authors" from Track.rb?
#belongs to multiple authors
has_many :track_authors
has_many :authors, through: :track_authors
has_one :original_author, through: :track_authors, conditions: { track_authors: { author: true } }
#bookmarked users
has_many :track_users
has_many :users, :through => :track_users
class TrackAuthor < ActiveRecord::Base
belongs_to :author, class_name: 'User', :foreign_key => "user_id"
belongs_to :creation, class_name: 'Track', :foreign_key => "track_id"
end
#authors track
has_many :track_authors
has_many :creations, through: :track_authors
has_many :original_creations, through: :track_authors, conditions: { track_authors: { author: true } }
#bookmarks track
has_many :track_users
has_many :tracks, :through => :track_users
@kibaekr
Copy link

kibaekr commented Feb 25, 2013

Temporarily solved by doing

 #track.rb
 def author
   track_authors.where(:author => true).first.author
 end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment