Automatically add sort order.

This commit is contained in:
BoHung Chiu 2020-10-31 10:17:24 +08:00
parent 690b219da0
commit 9abe9b71b6
1 changed files with 10 additions and 0 deletions

View File

@ -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