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

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -