Ruby on rails 在Rails中对深度嵌套的属性进行排序

Ruby on rails 在Rails中对深度嵌套的属性进行排序,ruby-on-rails,nested-attributes,Ruby On Rails,Nested Attributes,我想能够拖动和拖动应用程序模型,这是嵌套在类别模型 这是我试着跟随的铁路司机 #Category controller def move params[:apps].each_with_index do |id, index| Category.last.apps.update(['position=?', index+1], ['id=?', Category.last.id]) end render :nothing => true end 我可以用类似的东西对

我想能够拖动和拖动应用程序模型,这是嵌套在类别模型

这是我试着跟随的铁路司机

#Category controller
def move
  params[:apps].each_with_index do |id, index|
    Category.last.apps.update(['position=?', index+1], ['id=?', Category.last.id])
  end
  render :nothing => true
end
我可以用类似的东西对类别进行排序,但是因为我正在更新一个属性,所以我遇到了麻烦。这是我对类别列表排序的方式

def sort
  params[:categories].each_with_index do |id, index|
    Category.update_all(['position=?', index+1], ['id=?', id])
  end
  render :nothing => true
end
经过进一步检查,我需要的是能够同时更新所有应用程序,但我不能只更新App.update\u all,因为App是category的一个属性

我试着用

@category = Category.find(params[:id])
@app = @category.apps.all
但是,我没有传递类别id,所以它不知道它是哪个类别

在我看来是这样的

%ul#apps
  - for app in @category.apps
    - content_tag_for :li, app do
      %span.handle
        [drag]
    = h app.title

= sortable_element("apps", :url => move_categories_path, :handle => "handle")

感谢您的帮助。

原来这只是按位置对记录进行排序的问题。我在控制器中分类。所以对于嵌套属性模型,我在模型中对它们进行了排序:

has_many :apps, :dependent => :delete_all, :order => "position"
当我移动应用程序时,只需拨打电话即可更新位置

App.update_all(['position=?', index+1], ['id=?', id])
然后在模型中对它们进行相应的排序。事实证明,不需要传入类别的id,只需更新所有应用程序。但是,我担心它可能会慢一点,所以如果有人有更好的解决方案,我洗耳恭听

谢谢