Graceful way to iterate through two arrays and build a master array? params[:controller_id].each do |cid|
  params[:zone_number].each do |pzn|
    zone_ids &lt;&lt; Zone.find_by_controller_id_and_number(cid,pzn).id
  end
end


I have multiple controllers and multiples zone_numbers, and I would like to build a single array of zone_ids by iterating through controller and then zone per controller.

The code I wrote works - but there has to be a more eloquent way!
If you need to do the combination in Ruby (and I don't think you do; read on), you could do something like this, using Array#product

# just extracting these 2 params for clarity
controller_ids = params[:controller_id]
zone_numbers = params[:zone_number]

# product will give you every id/number combo,
# which is then mapped to a Zone record's ID
zone_ids = controller_ids.product(zone_numbers).map do |controller_id, number|
  Zone.find_by_controller_id_and_number(controller_id, number).id
end




However! I'd suggest less Ruby and more SQL for this.

# line-breaks are for clarity
zone_ids = Zone.where(
  'controller_id IN (?) AND number IN (?)', controller_ids, zone_numbers
).pluck(:id)


That should pluck just the id values from the Zones table without loading entire records.

While you can generate an array that contains every combination of controller ID and zone number, you'd still (with your current code) do separate DB queries for each of those combinations. And doing SQL queries in a loop is a sure sign you should look for another approach altogether (namely doing more in the DB layer, if possible).