开发者

rails acts_as_list position auto increment

开发者 https://www.devze.com 2023-04-13 02:55 出处:网络
I am using the acts_as_list gem but when creating new Object the position should automatic increase in size and order position ASC.

I am using the acts_as_list gem but when creating new Object the position should automatic increase in size and order position ASC.

LessonPage Model

class LessonPage < ActiveRecord::Base
    acts_as_list :scope => 'lesson_id = #{lesson_id}'
    default_scope order('position ASC')
end

Select

SELECT "lesson_pages".* FROM "lesson_pages" ORDER BY position ASC, "lesson_pages".id desc

Insert - first

INSERT INTO "lesson_pages" ("created_at", "lesson_id", "position", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Sun, 16 Oct 2011 11:55:27 CEST +02:00], ["lesson_id", 2], ["position", 1], ["updated_at", Sun, 16 Oct 2011 11:55:27 CEST +02:00]]

Second

INSERT INTO "lesson_pages" ("created_at", "lesson_id", "position", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Sun, 16 Oct 2011 11:57:15 CEST +02:00], ["lesson_id", 2], ["position", 2], 开发者_如何学JAVA["updated_at", Sun, 16 Oct 2011 11:57:15 CEST +02:00]]

Third

INSERT INTO "lesson_pages" ("created_at", "lesson_id", "position", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Sun, 16 Oct 2011 11:58:13 CEST +02:00], ["lesson_id", 2], ["position", 2], ["updated_at", Sun, 16 Oct 2011 11:58:13 CEST +02:00]]

Here it fails, because position should be 3 and not 2. How do I fix this? thanks!!


I think it was fixed by this commit, but there is no release after this fix. I think you should use the code from github.

use this:

gem 'acts_as_list', :git => 'https://github.com/swanandp/acts_as_list.git'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号