title: Retrieving unique associated models from an array of another model
body: <p>What is the recommended approach for finding multiple, unique associated models for a subset of another model? As an example, for a subset of users, determine unique artist models they have favorited.</p>
  
  <p>One approach is to grab the users from the database, then iterate them all quering for favorites and building a unique array, but this seems rather inefficient and slow.</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :favorites
  end
  
  class Artist &lt; ActiveRecord::Base
    has_many :favorites
  end
  
  class Favorite &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :artist
  end
  
  @users = User.find_by_age(26)
  # then determine unique favorited artists for this subset of users.
  </code></pre>