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

Popular posts from this blog

go - serving up pdfs using golang -

how to add preprocess loader in webpack 2 -

python - django admin: changing the way a field (w/ relationship to another model) is submitted on a form so that it can be submitted multiple times -