我加載的數據是這樣的:Rails - 如何計數從ActiveRecord加載的數據?
@cars = Car.includes(:car_services, :services).where(...)
當我需要從那裏得到的只有唯一的數據,我做這種方式:
<% @cars.uniq_by {|x| x["brand"]}.each do |car| %>
<%= I'd need to print out here the count of cars for 'car.brand' from '@cars' %>
我怎麼能這樣做?我在想這樣做這樣的事情:
@cars = Car.includes(:car_services, :services).where(...)
@cars2 = @cars
,然後在循環使用car.brand
和@cars2
搜索,但是這不會是可能很有效...什麼是實現這一目標的最佳途徑?
嗨!你能否詳細說明一下:「然後在循環中使用car.brand並在@ cars2中搜索,」 – 2014-12-02 19:19:05