【发布时间】:2018-03-09 14:14:07
【问题描述】:
我有一个 Rails 应用程序,我在其中实现了可排序的 Jquery,我想拖动图片并按顺序保存它们的位置。实际上它按顺序保存,但在某些时候它本身会改变顺序。
例如,
我有 16 张图片,这些图片的位置按预期保存,1,2,3,4,..,16。但是10分钟后我看到所有的位置都是166。但我不知道为什么会这样。
这里是代码,
<Picture id: 4213, name: nil, car_id: 202, created_at: "2017-03-31 17:34:48", updated_at: "2018-03-09 14:01:34", image: "24.jpg", position: 2>, #<Picture id: 4220, name: nil, car_id: 202, created_at: "2017-03-31 17:35:00", updated_at: "2018-03-09 14:01:34", image: "30.jpg", position: 3>..
从控制台这样保存;
D, [2018-03-09T17:16:02.139876 #65128] DEBUG -- : CarGroup Load (0.3ms) SELECT "car_groups".* FROM "car_groups" WHERE "car_groups"."name" = ? LIMIT 1 [["name", "All"]]
D, [2018-03-09T17:16:02.142094 #65128] DEBUG -- : User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2009]]
D, [2018-03-09T17:16:02.144736 #65128] DEBUG -- : Car Load (0.4ms) SELECT "cars".* FROM "cars" WHERE "cars"."id" = ? LIMIT 1 [["id", 202]]
D, [2018-03-09T17:16:02.148426 #65128] DEBUG -- : Picture Load (0.4ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."car_id" = ? AND "pictures"."id" = ? ORDER BY "pictures"."id" ASC LIMIT 1 [["car_id", 202], ["id", 8744]]
D, [2018-03-09T17:16:02.150712 #65128] DEBUG -- : (0.2ms) begin transaction
D, [2018-03-09T17:16:02.157362 #65128] DEBUG -- : SQL (0.6ms) UPDATE "pictures" SET "position" = ?, "updated_at" = ? WHERE "pictures"."id" = ? [["position", 1], ["updated_at", "2018-03-09 14:16:02.153131"], ["id", 8744]]
D, [2018-03-09T17:16:02.159729 #65128] DEBUG -- : (0.4ms) SELECT COUNT(*) FROM "pictures" WHERE (1 = 1) AND ("pictures"."position" = 1)
D, [2018-03-09T17:16:02.163624 #65128] DEBUG -- : (0.3ms) SELECT sql
FROM sqlite_master
WHERE name='index_pictures_on_car_id' AND type='index'
UNION ALL
SELECT sql
FROM sqlite_temp_master
WHERE name='index_pictures_on_car_id' AND type='index'
D, [2018-03-09T17:16:02.166731 #65128] DEBUG -- : SQL (1.0ms) UPDATE "pictures" SET "position" = ("pictures"."position" + 1), "updated_at" = '2018-03-09 14:16:02.165233' WHERE (1 = 1) AND ("pictures".id != 8744) AND ("pictures"."position" >= 1) AND ("pictures"."position" < 422)
D, [2018-03-09T17:16:02.180802 #65128] DEBUG -- : (13.0ms) commit transaction
D, [2018-03-09T17:16:02.182507 #65128] DEBUG -- : Picture Load (0.2ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."car_id" = ? AND "pictures"."id" = ? ORDER BY "pictures"."id" ASC LIMIT 1 [["car_id", 202], ["id", 8746]]
D, [2018-03-09T17:16:02.183784 #65128] DEBUG -- : (0.1ms) begin transaction
D, [2018-03-09T17:16:02.186040 #65128] DEBUG -- : SQL (0.3ms) UPDATE "pictures" SET "position" = ?, "updated_at" = ? WHERE "pictures"."id" = ? [["position", 2], ["updated_at", "2018-03-09 14:16:02.184761"], ["id", 8746]]
D, [2018-03-09T17:16:02.187173 #65128] DEBUG -- : (0.2ms) SELECT COUNT(*) FROM "pictures" WHERE (1 = 1) AND ("pictures"."position" = 2)
D, [2018-03-09T17:16:02.189225 #65128] DEBUG -- : (0.6ms) SELECT sql
FROM sqlite_master
WHERE name='index_pictures_on_car_id' AND type='index'
UNION ALL
SELECT sql
FROM sqlite_temp_master
WHERE name='index_pictures_on_car_id' AND type='index'
D, [2018-03-09T17:16:02.195676 #65128] DEBUG -- : SQL (3.9ms) UPDATE "pictures" SET "position" = ("pictures"."position" + 1), "updated_at" = '2018-03-09 14:16:02.190611' WHERE (1 = 1) AND ("pictures".id != 8746) AND ("pictures"."position" >= 2) AND ("pictures"."position" < 424)
D, [2018-03-09T17:16:02.201104 #65128] DEBUG -- : (4.2ms) commit transaction
D, [2018-03-09T17:16:02.202869 #65128] DEBUG -- : Picture Load (0.2ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."car_id" = ? AND "pictures"."id" = ? ORDER BY "pictures"."id" ASC LIMIT 1 [["car_id", 202], ["id", 8745]]
D, [2018-03-09T17:16:02.204345 #65128] DEBUG -- : (0.1ms) begin transaction
D, [2018-03-09T17:16:02.211590 #65128] DEBUG -- : SQL (3.1ms) UPDATE "pictures" SET "position" = ?, "updated_at" = ? WHERE "pictures"."id" = ? [["position", 3], ["updated_at", "2018-03-09 14:16:02.206634"], ["id", 8745]]
D, [2018-03-09T17:16:02.214258 #65128] DEBUG -- : (0.3ms) SELECT COUNT(*) FROM "pictures" WHERE (1 = 1) AND ("pictures"."position" = 3)
D, [2018-03-09T17:16:02.217133 #65128] DEBUG -- : (0.3ms) SELECT sql
FROM sqlite_master
WHERE name='index_pictures_on_car_id' AND type='index'
UNION ALL
SELECT sql
FROM sqlite_temp_master
WHERE name='index_pictures_on_car_id' AND type='index'
D, [2018-03-09T17:16:02.220316 #65128] DEBUG -- : SQL (1.5ms) UPDATE "pictures" SET "position" = ("pictures"."position" + 1), "updated_at" = '2018-03-09 14:16:02.218177' WHERE (1 = 1) AND ("pictures".id != 8745) AND ("pictures"."position" >= 3) AND ("pictures"."position" < 424)
D, [2018-03-09T17:16:02.228284 #65128] DEBUG -- : (4.2ms) commit transaction
D, [2018-03-09T17:16:02.230440 #65128] DEBUG -- : Picture Load (0.2ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."car_id" = ? AND "pictures"."id" = ? ORDER BY "pictures"."id" ASC LIMIT 1 [["car_id", 202], ["id", 8747]]
D, [2018-03-09T17:16:02.232011 #65128] DEBUG -- : (0.1ms) begin transaction
D, [2018-03-09T17:16:02.234995 #65128] DEBUG -- : SQL (0.5ms) UPDATE "pictures" SET "position" = ?, "updated_at" = ? WHERE "pictures"."id" = ? [["position", 4], ["updated_at", "2018-03-09 14:16:02.233093"], ["id", 8747]]
D, [2018-03-09T17:16:02.236920 #65128] DEBUG -- : (0.5ms) SELECT COUNT(*) FROM "pictures" WHERE (1 = 1) AND ("pictures"."position" = 4)
D, [2018-03-09T17:16:02.239218 #65128] DEBUG -- : (0.2ms) SELECT sql
FROM sqlite_master
WHERE name='index_pictures_on_car_id' AND type='index'
UNION ALL
SELECT sql
FROM sqlite_temp_master
WHERE name='index_pictures_on_car_id' AND type='index'
D, [2018-03-09T17:16:02.242578 #65128] DEBUG -- : SQL (1.4ms) UPDATE "pictures" SET "position" = ("pictures"."position" + 1), "updated_at" = '2018-03-09 14:16:02.240491' WHERE (1 = 1) AND ("pictures".id != 8747) AND ("pictures"."position" >= 4) AND ("pictures"."position" < 425)
D, [2018-03-09T17:16:02.248459 #65128] DEBUG -- : (4.6ms) commit transaction
D, [2018-03-09T17:16:02.250204 #65128] DEBUG -- : Picture Load (0.2ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."car_id" = ? AND "pictures"."id" = ? ORDER BY "pictures"."id" ASC LIMIT 1 [["car_id", 202], ["id", 8748]]
D, [2018-03-09T17:16:02.251196 #65128] DEBUG -- : (0.1ms) begin transaction
D, [2018-03-09T17:16:02.254066 #65128] DEBUG -- : SQL (0.4ms) UPDATE "pictures" SET "position" = ?, "updated_at" = ? WHERE "pictures"."id" = ? [["position", 5], ["updated_at", "2018-03-09 14:16:02.251964"], ["id", 8748]]
D, [2018-03-09T17:16:02.255436 #65128] DEBUG -- : (0.2ms) SELECT COUNT(*) FROM "pictures" WHERE (1 = 1) AND ("pictures"."position" = 5)
D, [2018-03-09T17:16:02.257680 #65128] DEBUG -- : (0.3ms) SELECT sql
FROM sqlite_master
WHERE name='index_pictures_on_car_id' AND type='index'
UNION ALL
SELECT sql
FROM sqlite_temp_master
WHERE name='index_pictures_on_car_id' AND type='index'
D, [2018-03-09T17:16:02.263112 #65128] DEBUG -- : SQL (1.9ms) UPDATE "pictures" SET "position" = ("pictures"."position" + 1), "updated_at" = '2018-03-09 14:16:02.259977' WHERE (1 = 1) AND ("pictures".id != 8748) AND ("pictures"."position" >= 5) AND ("pictures"."position" < 426)
D, [2018-03-09T17:16:02.269877 #65128] DEBUG -- : (5.7ms) commit transaction
D, [2018-03-09T17:16:02.271924 #65128] DEBUG -- : Picture Load (0.2ms) SELECT "pictures".* FROM "pictures" WHERE "pictures"."car_id" = ? AND "pictures"."id" = ? ORDER BY "pictures"."id" ASC LIMIT 1 [["car_id", 202], ["id", 8749]]
D, [2018-03-09T17:16:02.273540 #65128] DEBUG -- : (0.4ms) begin transaction
D, [2018-03-09T17:16:02.278737 #65128] DEBUG -- : SQL (0.7ms) UPDATE "pictures" SET "position" = ?, "updated_at" = ? WHERE "pictures"."id" = ? [["position", 6], ["updated_at", "2018-03-09 14:16:02.274866"], ["id", 8749]]
D, [2018-03-09T17:16:02.281097 #65128] DEBUG -- : (0.3ms) SELECT COUNT(*) FROM "pictures" WHERE (1 = 1) AND ("pictures"."position" = 6)
D, [2018-03-09T17:16:02.284165 #65128] DEBUG -- : (0.5ms) SELECT sql
FROM sqlite_master
WHERE name='index_pictures_on_car_id' AND type='index'
UNION ALL
SELECT sql
FROM sqlite_temp_master
WHERE name='index_pictures_on_car_id' AND type='index'
这就是我得到的位置和控制器动作;
Parameters: {"pic"=>["8744", "8746", "8745", "8747", "8748", "8749", "8750", "8751", "8752", "8753", "8754", "8755", "8756", "8757", "8758", "8759", "8760", "8761", "8762", "8763", "8764", "8765", "8766", "8767", "8768", "8769", "8770", "8771", "8772", "8773", "8774", "8775", "8776", "8777", "8778", "8779", "8780", "8781", "8782", "8783", "8784", "8785", "8786", "8787", "8788", "8789", "8790", "8791", "8792", "8793"], "locale"=>"tr", "car_id"=>"202"}
def sort
@car = Car.find(params[:car_id])
params[:pic].each_with_index do |id, index|
#@car.pictures.where(id: id).update_all(position: index+1)
pict = @car.pictures.where(id: id).first
pict.position = index + 1
pict.save
end
render nothing: true
end
【问题讨论】:
标签: ruby-on-rails position jquery-ui-sortable