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排序总是保存在集合中,我可以以某种方式禁用它吗?