我需要按2个属性对集合进行排序。一个是常规属性,我称之为attr
,另一个是ActiveStorage附件的名称。问题是,由于ActiveStorage定义关系的方式,我无法将其表连接到按其属性之一排序。
这大致就是我想要做的:
class Document < ApplicationRecord
has_one_attached :file
scope :sorted, -> { joins(:file).order(attr: :asc, 'files.filename': :asc) }
end
我知道files
不是表的名称,这只是一个例子。我修改了ActiveStorage的表名称,甚至尝试用ActiveStorage::Attachment和ActiveStorage::Blob手动定义关系,但都没有成功。
关于如何实现这一点有什么想法吗?
发布于 2018-08-04 11:24:53
要在DB服务器上对文档进行排序,您可以通过blobs的附件加入:
class Document < ApplicationRecord
has_one_attached :file
scope :ordered_by_filename, -> { joins(file_attachment: :blob).order("active_storage_blobs.filename ASC") }
end
或者,为了提高性能而去规格化。在documents表中保留文件名的副本:
class Document < ApplicationRecord
has_one_attached :file
before_save { self.filename ||= file.filename }
scope :ordered_by_filename, -> { order(filename: :asc) }
end
最后,在可行的情况下,考虑在应用程序中进行排序。(分页是可能不可行的一种情况。)在考虑可伸缩性的情况下,横向扩展应用程序服务器池通常比扩展数据库更容易:
class Document < ApplicationRecord
has_one_attached :file
delegate :filename, to: :file
def self.ordered_by_filename
# Use the with_attached_files scope to avoid N+1 queries.
with_attached_files.sort_by(&:filename)
end
end
https://stackoverflow.com/questions/51677069
复制相似问题