1
我的Thread
模型有很多Posts
。比方說,我想通過一個包含ID的數組重新排列帖子,我希望看到我的帖子按排序。排序收集來自陣列的ID
thread.posts.collect {|x| x.id} # => [1,2,3]
order = [2,3,1]
posts = thread.posts.sort_by {|x| order.index x.id}
posts.collect {|x| x.id} # => [2,3,1]
thread.update_attributes(:posts => posts) # => true
thread.posts.collect {|x| x.id} # => [1,2,3]
我在做什麼錯?按id排序總是保存在集合中,我可以以某種方式禁用它嗎?