Ruby on rails Rails回形针在使用自定义id保存之前进行插值

Ruby on rails Rails回形针在使用自定义id保存之前进行插值,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,在我的应用程序(Rails 5.2)中,我的模型使用UUID类型的id。 我又创建了一个字段:id\u server,这将是我想与回形针用于:id\u分区以创建多个文件夹的id(默认id\u分区与id字段一起使用,而不是与另一个字段一起使用) 我已经做到了: before_save do id_server = Photo.maximum(:id_server) + 1 end 创建下一个id_服务器 回形针: # paperclip has_attached_file :file, pa

在我的应用程序(Rails 5.2)中,我的模型使用UUID类型的id。 我又创建了一个字段:id\u server,这将是我想与回形针用于:id\u分区以创建多个文件夹的id(默认id\u分区与id字段一起使用,而不是与另一个字段一起使用)

我已经做到了:

before_save do
  id_server = Photo.maximum(:id_server) + 1
end
创建下一个id_服务器

回形针:

# paperclip
has_attached_file :file, path: "/upload/:class/:attachment/:id_server_partition/:style/:basename.:extension",
styles: { :tiny => "140x140>", :small => "160x240", :high => "640x960" }                   

validates_attachment :file, content_type: { content_type: ['image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png'] }
validates_attachment :file, size: { in: 0..5.megabytes }

# create multiple folders path with id_server
Paperclip.interpolates :id_server_partition do |attachment, style|
  attachment.instance.id_server_partition
end
def id_server_partition
  ("%09d".freeze % id_server).scan(/\d{3}/).join("/".freeze)
end
事实上,似乎永远不会在曲别针.interpolates之前调用before_save()

id_服务器在以下位置为零:

("%09d".freeze % id_server).scan(/\d{3}/).join("/".freeze)
("%09d".freeze % id_server).scan(/\d{3}/).join("/".freeze)
无法将nil转换为整数


我错过了什么?

奇怪的是,这是因为赛尔夫在:

before_save do
end
这样就可以工作了,id_服务器是在这里创建的:

self.id_server = Photo.maximum(:id_server) + 1
但不是这个:

id_server = Photo.maximum(:id_server) + 1
但在这里,不需要在id_服务器之前使用self: