This is more of a ruby array question than a rails specific question, but but lets say I have a database query that returns the products bought in the last 24 hours, in the form of: [0][id, product, category] [1][id, product, category].... Let''s also say I want to know what categories also had transactions in the past 24 hours. I could do another db lookup, but it doesn''t seem I need to because the data is in my product sales array above. My question is how do I combine the multi-dimensional arrays above by grouping by unique categories ? And is doing that more efficient than just doing another db query? If the answer is combining the array is more efficiently, I''ve looked at uniq and using the ruby set union "|" to combine them, but the multidimensional aspect is throwing me. Any suggestions or help would be appreciated. Steve
I''d say use the database. Unless you query from a remote database, the execution of the query uses far less resources then ruby trying to union an unique the array. Databases are made for this kind of stuff, so use them! ;) Flurin Steve Odom wrote:>This is more of a ruby array question than a rails specific question, >but but lets say I have a database query that returns the products >bought in the last 24 hours, in the form of: > >[0][id, product, category] >[1][id, product, category].... > >Let''s also say I want to know what categories also had transactions in >the past 24 hours. > >I could do another db lookup, but it doesn''t seem I need to because >the data is in my product sales array above. My question is how do I >combine the multi-dimensional arrays above by grouping by unique >categories ? And is doing that more efficient than just doing another >db query? > >If the answer is combining the array is more efficiently, I''ve looked >at uniq and using the ruby set union "|" to combine them, but the >multidimensional aspect is throwing me. > >Any suggestions or help would be appreciated. > >Steve >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >
Try using inject. On 17-jun-2005, at 14:16, Steve Odom wrote:> This is more of a ruby array question than a rails specific question, > but but lets say I have a database query that returns the products > bought in the last 24 hours, in the form of: > > [0][id, product, category] > [1][id, product, category].... > > Let''s also say I want to know what categories also had transactions in > the past 24 hours. > > I could do another db lookup, but it doesn''t seem I need to because > the data is in my product sales array above. My question is how do I > combine the multi-dimensional arrays above by grouping by unique > categories ? And is doing that more efficient than just doing another > db query? > > If the answer is combining the array is more efficiently, I''ve looked > at uniq and using the ruby set union "|" to combine them, but the > multidimensional aspect is throwing me. > > Any suggestions or help would be appreciated. > > Steve > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- Julian "Julik" Tarkhanov