title: Get 9 random photos from a Collection
body: <p>My associations are as follows:</p>
  <p>Collection:</p>
  <pre><code>has_many :children, class_name: &quot;Collection&quot;, foreign_key: &quot;parent_id&quot;
  belongs_to :parent, class_name: &quot;Collection&quot;, foreign_key: &quot;parent_id&quot;, optional: true
  has_many :albums, dependent: :nullify
  </code></pre>
  <p>Album:</p>
  <pre><code>has_many :photos, dependent: :destroy
  belongs_to :collection, optional: true
  </code></pre>
  <p>Photo:</p>
  <pre><code>belongs_to :album
  </code></pre>
  <p>What I wish to achieve is to, from a collection, pull 9 random photos from ANY Album either directly within the calling Collection or in any sub Collection.</p>
  <p>My existing method only seems to pull from albums that are direct children of the calling Collection and that's not quite the effect I am after but unsure how to adjust it.</p>
  <pre><code>  def photos_for_icon
      Photo.joins(:album).where(albums: { collection_id: self.id }).order(&quot;RANDOM()&quot;).first(9)
    end
  </code></pre>
  <p>Anyone have any ideas?</p>
  <p>Thanks,
  Neil</p>