acts_as_list的用法

acts_as_list

功能:排序
使用难度:简单
fork数:295

安装:
gem 'acts_as_list'

使用:

rails g migration AddPositionToTodoItem position:integer
rake db:migrate

class TodoList < ActiveRecord::Base
  has_many :todo_items, -> { order(position: :asc) } 
end

class TodoItem < ActiveRecord::Base
  belongs_to :todo_list
  acts_as_list scope: :todo_list
end

todo_list = TodoList.find(...)
todo_list.todo_items.first.move_to_bottom
todo_list.todo_items.last.move_higher

注意:

建立方法move_higher:

def move_higher
  todo_list.todo_items.first.move_higher
end

建立路径:

resources todo_items do
  get :move_higher
end

建立方法move_to_bottom:

def move_to_bottom
  todo_list.todo_items.first.move_to_bottom
end

建立路径:

resources todo_items do
  get :move_to_bottom
end
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容