From 9abe9b71b6d4f5858d26f673a4846a56c3be0c98 Mon Sep 17 00:00:00 2001 From: bohung Date: Sat, 31 Oct 2020 10:17:24 +0800 Subject: [PATCH] Automatically add sort order. --- app/models/album.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/models/album.rb b/app/models/album.rb index 325b4a6..66486b4 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -20,6 +20,16 @@ class Album has_many :album_colors, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :album_images, :allow_destroy => true accepts_nested_attributes_for :album_colors, :allow_destroy => true + before_save do |record| + if record.order.nil? || record.order == -1 + topest_order = Album.all.pluck(:order).sort{|a,b| a.to_i<=>b.to_i}.first + if topest_order.nil? || topest_order == 0 + record.order = -2 + else + record.order = topest_order - 1 + end + end + end def self.find_by_param(input) self.find_by(uid: input) end