2

I am querying my database to come up with a list of activities, and I would like them ordered in descending order by the updated_at attribute. I get this list of activities by going through two other models and doing maps, i.e.:

@listings = current_user.listings.includes(:deals)
map_of_listings = @listings.map { |listing| listing.deals }
@deals = map_of_listings.flatten

map_of_deals = @deals.map { |deal| deal.activities }
activities_array = map_of_deals.flatten

The problem for me now is that the activities in the array are grouped by deal, instead of being listing in descending order by when they were last updated. How can I order this array by the updated_at attribute of activity?

1
  • Misspelled "ordering" in the title btw ;) Commented May 30, 2012 at 18:58

1 Answer 1

2

If you're on Ruby 1.9, you could try

activities_array.sort_by(&:updated_at)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.