2012-02-24 18 views
0

試圖通過gette()

或者我認爲多數民衆贊成在我想要的。我選擇的每一行,並改變它的Sort.sortorder到數組值,所以我想用racksh與西納特拉迭代Datamapper得到更新單獨的行

sqlite> select * from sorts; 
    515|1|2012-02-24T14:44:07-05:00 
    516|2|2012-02-24T14:44:07-05:00 
    517|3|2012-02-24T14:44:07-05:00 
    518|4|2012-02-24T14:44:07-05:00 
    sqlite> 

測試代碼歸結爲迄今

adkjsd = [1,2,3,4] 

adkjsd.each do |jk| 
    puts jk + 1 
    Sort.all.update(:sortorder => jk) # true updates all rows to 4 the last in the array 
    Sort.get(516).update(:sortorder => 0) # only updates that row 
end 

Sort.all.update(:sortorder => jk) # true updates all rows to 4 the last in the array 
#output 
sqlite> select * from sorts; 
515|4|2012-02-24T14:44:07-05:00 
516|4|2012-02-24T14:44:07-05:00 
517|4|2012-02-24T14:44:07-05:00 
518|4|2012-02-24T14:44:07-05:00 
sqlite> 

Sort.get(516).update(:sortorder => 0) # only updates that row 
#output 
sqlite> select * from sorts; 
515|4|2012-02-24T14:44:07-05:00 
516|0|2012-02-24T14:47:03-05:00 
517|4|2012-02-24T14:44:07-05:00 
518|4|2012-02-24T14:44:07-05:00 
sqlite> 

回答

0

也許這就是你想要的?

adkjsd = [1,2,3,4] 
Sort.all.each_with_index do |sort, i| 
    sort.sortorder = adkjsd[i] 
    sort.save 
end 

我仍然在使用數組那裏,因爲我不知道該值,您試圖設置將始終是相同的。如果它只是從一開始最多的行數,你可以這樣做:

Sort.all.each_with_index do |sort, i| 
    sort.sortorder = i + 1 
    sort.save 
end 
+0

!!!!!你贏得了一百萬的互聯網!謝謝!這是一個學習冒險,在我的問題堆棧中與前面的問題結合起來。 http://stackoverflow.com/questions/9425096/sinatra-ruby-iterate-over-an-array-in-a-hash-to-send-jquery-sortable-to-database/9​​426068#comment11918408_9426068 再次謝謝你! – tripdragon 2012-02-24 20:42:53

+0

不客氣:) – 2012-02-24 20:46:08