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

4

2 回答 2

0

将用户首选项存储在单独的表中,然后使用 @categories 加入 userOrderTable

于 2012-08-01T15:27:43.467 回答
0

另一种方法可能是创建一个空数组并遍历类别:

@categories = []
current_user.order.each do |category_id|
 @categories << Category.find(category_id)
end
于 2012-08-01T15:37:33.573 回答