简体   繁体   English

Ruby on Rails-从最后一个对象增加1

[英]Ruby on Rails - Increment by 1 from last object

I am building a user gallery application, where user can create albums and upload images to their album. 我正在构建一个用户图库应用程序,用户可以在其中创建相册并将图像上传到他们的相册。 I am using nested forms for images. 我正在为图像使用nested forms

In my images database, I have a column called sort_order . 在我的images数据库中,我有一列称为sort_order I want to use sort_order to manage the sorting and showing user, for ex: he is seeing 3 of 12 images, where 3 is number of sort_order and 12 is number of total images in that album. 我想使用sort_order来管理排序和显示用户,例如:他正在查看12张图片中的3张,其中3张sort_order数量,而12张是该相册中总图像的数量。

sort_order is integer and default: 0 . sort_orderintegerdefault: 0 What I looking for is to increase/ increment number of sort_order for each image based on previous sort_order of last image in same album. 我寻找的是增加/ increment的数量sort_order根据以往的每个图像sort_order在同一专辑最后的形象。

For ex: image 1 will have sort_order: 1 , image 2 will then have previous images sort_id +1 => sort_order: 2 and so on. 例如: 图像1将具有sort_order: 1 ,然后图像2将具有先前的图像sort_id +1 => sort_order: 2 ,依此类推。

Here is the code I am using now: 这是我现在使用的代码:

after_create :previous_image

  def previous_image

    img = user.images.order("id ASC")

    img.each do |image|
      image.increment!(:sort_order, self.sort_order + 1)
    end

  end

This does the work but in wrong sort. 这可以完成工作,但排序错误。

id      sort_order
10          3
11          2
12          1

instead of having, id: 10 sort_order: 1 , its getting 3 . 而不是id: 10 sort_order: 1 ,它得到3

This is the log: 这是日志:

  SQL (0.5ms)  UPDATE "images" SET "sort_order" = ?, "updated_at" = ? WHERE "images"."id" = ?  [["sort_order", 1], ["id", 398]]

  user Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 160]]

  Slide Load (0.1ms)  SELECT "images".* FROM "images" WHERE "images"."user_id" = ?  ORDER BY id DESC  [["user_id", 160]]

  SQL (0.1ms)  UPDATE "images" SET "sort_order" = ?, "updated_at" = ? WHERE "images"."id" = ?  [["sort_order", 1], ["id", 399]]

  SQL (0.1ms)  UPDATE "images" SET "sort_order" = ?, "updated_at" = ? WHERE "images"."id" = ?  [["sort_order", 2], ["id", 398]]

  user Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 160]]

  Slide Load (0.1ms)  SELECT "images".* FROM "images" WHERE "images"."user_id" = ?  ORDER BY id DESC  [["user_id", 160]]

  SQL (0.1ms)  UPDATE "images" SET "sort_order" = ?, "updated_at" = ? WHERE "images"."id" = ?  [["sort_order", 1], ["id", 400]]

  SQL (0.1ms)  UPDATE "images" SET "sort_order" = ?, "updated_at" = ? WHERE "images"."id" = ?  [["sort_order", 2], ["id", 399]]

  SQL (0.1ms)  UPDATE "images" SET "sort_order" = ?, "updated_at" = ? WHERE "images"."id" = ?  [["sort_order", 3], ["id", 398]]

It starts from bottom and not top. 它从底部开始,而不是顶部。 How can I fix it? 我该如何解决?

If there is another way to do it without having the sort_order column, I am open to it. 如果还有另一种方法可以不用sort_order列,那么我可以接受。

After receiving the form data, add an index (starting from 1) to the images array. 收到表单数据后,向图像数组添加一个索引(从1开始)。

Considering this is your images array from the form: 考虑到这是来自表单的图像数组:

images = [{name: 'Image 1', data: 123}, {name: 'Image 2', data: 456}]

Use with_index(1) to attach the sort order to every element: 使用with_index(1)将排序顺序附加到每个元素:

images.map.with_index(1).to_a images.map.with_index(1).to_a

=> [[{:name=>"Image 1", :data=>123}, 1], [{:name=>"Image 2", :data=>456}, 2]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM