2010-12-22 34 views
5

我通過將以下行的Gemfile和「包安裝」安裝:有沒有人得到acts_as_list在rails 3上工作?

gem 'acts_as_list', '>= 0.1.0' 

然而,當我嘗試使用它,是因爲預期的效果不是:

technician.move_to_top #works => position = 1 
technician.move_to_bottom #does not work properly; also makes position = 1 
technician.move_higher #does not work; returns nil 
technician.move_lower #does not work; also returns nil 

這是否插件只是不使用導軌3或我錯過了一個步驟?

這裏是我使用的代碼:

class WorkQueue < ActiveRecord::Base 
    has_many :technicians, :order => "position" 
end 

class Technician < ActiveRecord::Base 
    belongs_to :work_queue 
    acts_as_list :scope => "work_queue_id" #I also tried using work_queue 
end 

這是控制檯:

wq = WorkQueue.new 
technician = Technician.last 
wq.technicians << technician 
+0

可能是位置不是連續的數字? – lulalala 2012-07-02 10:19:16

+0

使用gem acts_as_lists https://github.com/swanandp/acts_as_list時面臨同樣的問題。你有沒有找到如何解決它。我正在使用** Rails 3.2.2 ** – 2012-10-24 13:25:15

回答

9

不要使用「acts- as-list「寶石,因爲這寶石很長時間不會更新。

試試這個:

rails plugin install git://github.com/swanandp/acts_as_list.git 
3

acts_as_list 0.2.0爲我工作在Rails的3.2.11關於Ruby 1.9.3。但是,添加任務的語法會導致問題。我使用list.tasks.create()來代替。

下面是一個簡單的測試:

test "acts_as_list methods" do 
    list = ToDoList.create(description: 'To Do List 1') 

    task1 = list.tasks.create(description: 'Task 1') 
    task2 = list.tasks.create(description: 'Task 2') 
    task3 = list.tasks.create(description: 'Task 3') 

    assert_equal 3, list.tasks.count 
    assert_equal task1.id, list.tasks.order(:position).first.id 
    assert_equal task3.id, list.tasks.order(:position).last.id 

    # Move the 1st item to the bottom. The 2nd item should move into 1st 
    list.tasks.first.move_to_bottom 

    assert_equal 3, list.tasks.count 
    assert_equal task2.id, list.tasks.order(:position).first.id 
    assert_equal task1.id, list.tasks.order(:position).last.id 
end 

當創建無關的to_do_list新任務,acts_as_list將分配一個位置範圍的對抗to_do_list_id ==爲零。稍後使用< <將現有任務添加到to_do_list不會更新任務位置,因此acts_as_list會對位置感到困惑。

檢查您的test.log以查看acts_as_list生成的SQL語句,以清楚瞭解特定應用中發生的情況。

由於看起來您的技術人員在創建work_queue任務後分配,您可能需要在調用'< <'後手動設置或重新計算位置。您也可以考慮將acts_as_list移動​​到您的TechnicianWorkQueue模型,以便僅在創建技術人員和WorkQueue之間的關係時調用acts_as_list。

相關問題