1

My site allows users to change the order in which categories and what categories are displayed, and it stores that order as an array in the db. so I have:

@categories = Category.where("id IN (?)", current_user.order)

the problem is order is something like [2,4,3,6,1] and then @categories has order [1,2,3,4,6]. I want to order the @categories so it matches the order. Any help would be appreciated

Bishop
  • 296
  • 8
  • 19
  • I ended up going with the solution listed [here](http://stackoverflow.com/questions/396748/ordering-by-the-order-of-values-in-a-sql-in-clause) – Bishop Aug 01 '12 at 15:57

2 Answers2

0

Store user preferences in a separate table and then JOIN userOrderTable with @categories

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
0

Another approach might be to create an empty array and iterate over the Categories:

@categories = []
current_user.order.each do |category_id|
 @categories << Category.find(category_id)
end
neon
  • 2,811
  • 6
  • 30
  • 44