dimanche 27 mai 2018

Rails: More efficient way of my Select and Each loop?

I currently use the following loops to cycle through a product list. If the item[:product_id] exists in my Products table then I want to grab the length, width and height value. What I currently have is:

   params[:items].each do |item|  
      item = item[:product_id]
       productDimensions = Product.select(:length, :width, :height).where(product_id: item)
      productDimensions.each do |product|  
        return length = product.length
        return width = product.width          
        return height = product.height
      end
   end

Now this works, but it feels like a very cumbersome way to do it.

Is there a better way I can be selecting these values?

Aucun commentaire:

Enregistrer un commentaire