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 -

python - Best design pattern for collection of objects -

r - Using `bbmle:mle2` with vector parameters (already works using `optim`) -