mysql - Rails Active Record 查询 - 分类,根据 'updated_at' 值排序

标签 mysql ruby-on-rails ruby ajax will-paginate

在我的 Rails 网络应用程序(Rails 3.2.9Ruby 1.9.3)中,有一个特定于每个用户的文章仪表板页面,他/她创造了上市。有一些主要类别,应在这些类别下列出文章。文章详情以表格结构显示。

Sl#     Name           Reference       Updated At

Category 1
1       Article 1      abc abc         05/16/2016 11:12AM
2       Article 2      pqr stu         05/14/2016 10:11PM
3       Article 3      abc abc         05/13/2016 05:05AM

Category 2
4       Article 4      abc abc         05/16/2016 11:00AM
5       Article 5      pqr stu         05/15/2016 06:41PM

Category 3
6       Article 6      abc abc         05/16/2016 10:12AM
7       Article 7      pqr stu         05/01/2016 09:52PM
8       Article 8      abc abc         05/12/2016 12:05PM

所以,这个用户创建了 8 篇文章,比如说“Ramu”,它们被归类在 3 个类别下。文章可以随时更新。第一条规则是

The category which has the latest updated article needs to be shown on top and so on

所以,在这里你可以看到 Category 1Article 1 已经更新到最新,然后是 Category 2 的 Article 4。所以,Category 1,然后是 Category 2等等

The Second rule is that Articles under a category should be sorted based on the updated_at value in DESC order.

@articles =
  @current_user.articles.order('updated_at DESC').paginate(page: params[:page], per_page: 10)

此查询无法按预期顺序获得结果,因为与其他类别的文​​章相比,具有最新更新文章的类别中的文章可能具有较小的 updated_at 值。但是最新更新的文章连同类别 1 的其他文章将列在顶部等等。

@articles =
  @current_user.articles.order('category_id, updated_at DESC').paginate(page: params[:page], per_page: 10)

上面的也是不可用的,因为它首先根据 category_id 进行排序。

谁能帮我得到一个优化的 MySQL 查询。谢谢。

最佳答案

class Category < ActiveRecord::Base
  has_many :articles
  #has_many :ordered_articles , -> {order(updated_at: :desc)}, class_name: 'Article', foreign_key: 'category_id'
  has_many :ordered_articles , order: "updated_at desc", class_name: 'Article', foreign_key: 'category_id'
end

class Article < ActiveRecord::Base
  belongs_to :category
end

这里是你的问题的解决方案

@categories = Category.select("categories.id,categories.name  , max(articles.updated_at) as last_article").joins(:articles).group("categories.id").order("last_article desc").includes(:ordered_articles).where("articles.user_id = ?",current_user.id)

所有的魔法都在这里发生。

这将按您想要的顺序返回所有类别。 它将返回按文章 updated_at 列排序的所有类别。

其次,我在 category 类中添加了一个新的 has_many 关联。 它由 ordered_articles 命名。它就像普通的 has_many :articles 关联与有序文章的好处。它将返回由 updated_at desc 排序的 category 的所有 articles。如果需要,您可以在原始 has_many 关联上添加这些 order 功能。

现在所有的 categories 都可以在 @categories 实例变量中使用。遍历它并通过对其调用 ordered_articles 获取该类别中的所有 articles。我-我

@categories.each do |category|
 puts category.name
  category.ordered_articles.each do |article|
  puts article.title
  end
end

这就是您想要的输出。您可以根据需要获取其他属性。我假设您的 Category 有一个名为 name 的列,而 article 有一个列 title

您可以将其更改为您的实际列。

P.S: 我们可以跳过新的 has_many 关联可以调用 articles 在每个 category 对象上使用 order_by 子句例如 category.articles.order(updated_at: :desc) 但这会导致 N+1 查询困境。因此,为了eager load每个类别的相应文章,我添加了一个新的关联,它以我们想要的顺序返回文章

关于mysql - Rails Active Record 查询 - 分类,根据 'updated_at' 值排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37260822/

相关文章:

php - 如何在php中生成从F-00001开始的唯一tag_id

mysql - 为什么大表的表级锁定优于行级锁定?

ruby-on-rails - 使用 ruby​​ on rails 的在线音频流

javascript - 如何在字符串中获得可能重叠的匹配项

Ruby Headless gem 屏幕尺寸未得到遵守

Ruby:如何获取与条件匹配的前 x 数组元素的索引?

PHP:SQL 中的记录未更新

PHP MD5(MD5) 哈希

javascript - 打印另一页的页面上的打印按钮

ruby-on-rails - 使用 mongoid-elasticsearch gem 对搜索结果进行排序