Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Active storage Rails 5.2中按文件名订购多个图像_Ruby On Rails_Rails Activestorage - Fatal编程技术网

Ruby on rails 在Active storage Rails 5.2中按文件名订购多个图像

Ruby on rails 在Active storage Rails 5.2中按文件名订购多个图像,ruby-on-rails,rails-activestorage,Ruby On Rails,Rails Activestorage,我有许多附加的图片后模型的关系。我想在后期显示页面上显示图像时按其文件名排序。如何按文件名订购 例如: <% @post.images.order("id DESC").each do |image| %> 是否可以通过活动存储\u blob.filename或其他方式按文件名排序 将has\u many\u attached宏添加到类时,实际上会将以下关系添加到类中 class Post < ApplicationRecord has_many :imag

我有许多附加的图片后模型的关系。我想在后期显示页面上显示图像时按其文件名排序。如何按文件名订购

例如:

<% @post.images.order("id DESC").each do |image| %>


是否可以通过活动存储\u blob.filename或其他方式按文件名排序

将has\u many\u attached宏添加到类时,实际上会将以下关系添加到类中

  class Post < ApplicationRecord
     has_many :image_attachments, -> { where(name: 'image') }, class_name: "ActiveStorage::Attachment", as: :record, inverse_of: :record, dependent: false
     has_many :image_blobs, through: :image_attachments, class_name: "ActiveStorage::Blob", source: :blob
  end
现在,所有图像都将按文件名排序,但是因为连接实际上不会加载任何数据,我假设您在视图中引用了我们也可以使用的文件名

   <% @post.images.includes(:blobs).references(:blobs).order('active_storage_blobs.filename ASC').each do |image| %>


使用一个查询同时加载图像和
ActiveStorage::Blob
数据,以避免原始版本可能出现的n+1问题。

将has\u many\u ATTABLED宏添加到类时,实际上会将以下关系添加到类中

  class Post < ApplicationRecord
     has_many :image_attachments, -> { where(name: 'image') }, class_name: "ActiveStorage::Attachment", as: :record, inverse_of: :record, dependent: false
     has_many :image_blobs, through: :image_attachments, class_name: "ActiveStorage::Blob", source: :blob
  end
现在,所有图像都将按文件名排序,但是因为连接实际上不会加载任何数据,我假设您在视图中引用了我们也可以使用的文件名

   <% @post.images.includes(:blobs).references(:blobs).order('active_storage_blobs.filename ASC').each do |image| %>


使用单个查询同时加载图像和
ActiveStorage::Blob
数据,以避免原始版本可能出现的n+1问题。

如果使用ActiveStorage设置类,如:

class Post < ApplicationRecord
  has_many_attached :images
end

如果使用ActiveStorage设置类,如:

class Post < ApplicationRecord
  has_many_attached :images
end
对于Rails 6,我需要

@post.images.includes(:blob).references(:blob).order('active_storage_blobs.filename ASC')。每个do|image|
# ...
结束
我需要的Rails 6

@post.images.includes(:blob).references(:blob).order('active_storage_blobs.filename ASC')。每个do|image|
# ...
结束

也许你可以试试
post.images.joins(:blob).order('active\u storage\u blobs.filename ASC')
?Perfect@engineersmnky。我需要加强我的比赛。成功了。公平竞争。回答一下,我会确认的,伙计。谢谢。也许你可以试试
post.images.joins(:blob).order('active\u storage\u blobs.filename ASC')
?Perfect@engineersmnky。我需要加强我的比赛。成功了。公平竞争。回答一下,我会确认的,伙计。谢谢。我认为这现在类似于
@post.images\u attachments.joins(:blob).
我认为这现在类似于
@post.images\u attachments.joins(:blob).