activerecord - rails 3 + active record: is there a clever way to "round robin" the order of records -


(the actual numbers 100x larger, easier explain follows)

we have record set of 100 reviews 5 restaurants. restaurants have 3 or 4 reviews, have 20-30 reviews.

currently have simple report arranges them newest oldest:

@reviews = review.joins(:store).  where('reviews.published= ?',true).  order("reviews.created_at desc") 

for 1 of report views, need display reviews in round-robin order ensures each restaurant gets equal coverage @ top of list.

most recent restaurant #a  recent restaurant #b  ... recent restaurant #e 

then repeat next-most-recent #a, #b,... #e

etc

ideally order of round-robin restaurant has total reviews.

it's not hard slow way in code, i'm wondering if there's faster/better activerecord approach?

===

is there way (a) sort restaurant, (b) create temporary "counter" column counts 1 n each entry each restaurant, (c) re-sort on counter column? it.

for example, after (a) , (b) recordset be

a , <some review>, 1 , <some review>, 2 ... , <some review>, 23  b , <some review>, 1 b , <some review>, 2 ... b , <some review>, 11  c , <some review>, 1 c , <some review>, 2 ... c , <some review>, 9 

if sort recordset on counter, round-robin a,b,c, etc

you can add column in db last_displayed, update each display of restaurant , sort .order('last_displayed').

i think use updated_at column, touch record after displaying it, , sort column - way won't need update db structure


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -