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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -