ruby - How to reorder activerecord array based on array of ids -
i have existing activerecord array, gotten following:
@posts = post.where(["created_at > ?", n_days.days.ago]) and order array based on value needs calculated in real-time multiple columns:
posts_array = posts.map { |p| [p.id, f(t, p.x,p.y)] } this gives array [[1,20],[2,11],[3,14], ...], sort based on function value give [[1,20],[3,14],[2,11],...], , order_array want: [1,3,2,...].
what best way reorder original @posts array based on new order_array [1,3,2,...]? don't want touch database again if possible, since information contained in @posts array.
is there straightforward way this?
if don't need posts_array array do
@posts = post.where(["created_at > ?", n_days.days.ago]) @posts.sort_by! { |p| f(t, p.x, p.y) }
Comments
Post a Comment