title: Rails - how to fetch all (unique) data through has_many association?
body: <p>I've these models:</p>
  
  <p><strong>cars</strong>:</p>
  
  <pre><code>class Car &lt; ActiveRecord::Base 
    has_many :car_services, dependent: :destroy
    has_many :services, through: :car_services
  end
  </code></pre>
  
  <p><strong>cars</strong>:</p>
  
  <pre><code>class CarService &lt; ActiveRecord::Base
    belongs_to :car
    belongs_to :service
  end
  </code></pre>
  
  <p><strong>service</strong></p>
  
  <pre><code>class Service &lt; ActiveRecord::Base
    has_many :car_services
    has_many :cars, through: :car_services
  end
  </code></pre>
  
  <p>I have in a variable saved all cars that are matching a criteria, like:</p>
  
  <pre><code>@cars = Car.where('shipped_at = ?', params[:shipped])
  </code></pre>
  
  <p>Now is in <code>@cars</code> saved several cars and each car has a list of services. I am trying to get a unique list of all services for these cars and in the format <code>Service A, Service O, Service P</code>.</p>
  
  <p>How to do that? There's a way to iterate through all <code>@cars</code>, then save each service into an array, than call <code>.uniq_by {|x| x["service_id"]}</code> on that array and then print it out.</p>
  
  <p>Is there a more effective way to do it? I think like in this work-flow is too many steps.</p>
  
  <p>Thank you in advance.</p>