mysql - How can I include a field that is not part of the group clause in a ActiveRecord query? -


(ruby 1.9.3-p429; rails 3.0.6) recent changes way queue our tests have rendered our last_ids scope:

last_ids = tests.where('end_time != 0').                  group(:condition1, :condition2, :condition3, :condition4).                  select('max(id) id') 

logically obsolete. previously, tests queued , completed in same order test record created maximum id each grouping latest test record. now, efficiency, have queuing algorithm no longer guarantees order of tests. have added "end_time" column test table. solution, far slow use:

max_end_times = tests.where('end_time != 0').                         group(:condition1, :condition2, :condition3, :condition4).                         .maximum(:end_time) 

which yields hash: {[:condition0, :condition1, :condition2, :condition3]=>:endtime}. follows:

hash.each_pair |key, value|   last_ids << tests.select(:id).                       where(condition0: [key[0]]).                       where(condition1: [key[1]]).                       where(condition2: [key[2]]).                       where(condition3: [key[3]]).                       where(end_time: [value]).                       map(&:id)[0] end 

this works, entirely slow.

so...does know how match maxumum(:end_time) given grouping return :id's? (all need database :ids of lastest test each grouping).

edit: clarification.

activerecord::calculations#pluck

http://api.rubyonrails.org/classes/activerecord/calculations.html#method-i-pluck

edit: if don't have #pluck, still in 1 query & #map with:

tests.where('end_time != 0').              group(:condition1, :condition2, :condition3, :condition4).              select('id, max(created_at)').map(&:id) 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -