Get all unique associations in Rails 5 -
i have , object rides , ride belongs_to company.
i list of rides
@rides = ride.where(...)
what need store companies of ride in @companies
want have every company once, if 2 rides have same company.
you can unique companies of rides below:
@rides = ride.includes(:company).where(...) @companies = @rides.map(&:company).uniq
note: includes
load companies in single query associated resulting rides
(prevents n+1 query problem).
Comments
Post a Comment