简体   繁体   English

在created.at排序的blog.articles数组中的正确位置插入重新发布的文章

[英]insert reblogged articles in the right place in blog.articles array ordered by created_at

EDIT: Actually I just decided against my way of reblogging and instead decided to implement it by copying and making a whole new record from the article and then add a reblogged: true column to it. 编辑:实际上,我只是决定不采用重新博客的方式,而是决定通过从文章中复制并制作一条新记录来实现它,然后向其中添加一个relogger:true列。 A bit cleaner in logic in my opinion and it avoids all the problems listed below. 我认为逻辑上更简洁一点,它避免了下面列出的所有问题。


In a blog app I've implemented the possibility of reblogging an article of someone else's blog through a join reblog model with two foreign keys. 在一个博客应用程序中,我实现了通过带有两个外键的join reblog模型来重新撰写其他人的博客文章的可能性。

In my blogs show action I am then adding blog.articles with blog.reblogged_articles to one single array of articles. 然后,在我的博客展示动作中,我将blog.articles和blog.relogger_articles添加到单个文章数组中。 I then sort this array by the created_at attribute of each element. 然后,我根据每个元素的created_at属性对该数组进行排序。

However, for the reblogged articles, I'd rather like to use the created_at attribute of their reblog join model, so that a reblogged article will initially appear on the top of the blog. 但是,对于重新发布的文章,我希望使用其重新博客连接模型的created_at属性,以便重新发布的文章最初会出现在博客顶部。 A reblogged article should always get its position in the blog as if it was created by the owner of the blog. 重新发布的文章应始终在博客中获得其位置,就像它是由博客所有者创建的一样。 But currently, a reblogged article might appear on the bottom of the blog, if the article was created 3 years ago. 但目前,如果该文章是3年前创建的,则该文章的底部可能会出现重新发布的文章。

I probably need to modify the created_at attributes of my reblogged_articles array before I put the two arrays together. 在将两个数组放在一起之前,我可能需要修改relogger_articles数组的created_at属性。 But how do I do that? 但是我该怎么做呢?

Edit: Just an example to clarify: 编辑:只是一个例子来澄清:

Blog id 1: 博客ID 1:
Article id 1, created_at: A year ago. 文章ID 1,created_at:一年前。
Article id 2, created_at: A month ago. 文章ID 2,created_at:一个月前。
Article id 3, created_at: A week ago. 文章ID 3,created_at:一周前。
Article id 4, created_at: A day ago. 文章ID 4,created_at:前一天。

Blog id 2: 博客ID 2:
Article id 5, created_at: A day ago. 文章ID 5,created_at:前一天。
Article id 6, created_at: An hour ago. 文章ID 6,created_at:一个小时前。
Article id 7, created_at: 30 minutes ago. 文章ID 7,created_at:30分钟前。
Article id 8, created_at: About 10 minutes ago. 文章ID 8,created_at:大约10分钟前。

Now, Blog 2 reblogs Article 1 of Blog 1. I want this article to be displayed on the top of Blog 2, as if it has been created just now. 现在,博客2重新撰写了博客1的文章1。我希望将这篇文章显示在博客2的顶部,就像它是刚刚创建的一样。 However, since that article in fact was created a year ago, it will be displayed on the bottom of Blog 2. How can I get around this? 但是,由于该文章实际上是在一年前创建的,因此将显示在Blog 2的底部。如何解决此问题?

What's the smartest approach to bring reblogged articles in their "correct" position based upon the created_at of the join model Reblog instead of the created_at of the article itself? 根据联接模型Reblog的created_at(而不是文章本身的created_at)将重新发布的文章置于“正确”位置的最聪明的方法是什么?

The easiest solution is probably just adding a last_activity_date column to your article model. 最简单的解决方案可能只是将last_activity_date列添加到您的文章模型中。 Whenever an article is reblogged, update the corresponding article's last_activity_date. 每当文章被重新发送时,更新相应文章的last_activity_date。 Then, when you're getting articles, Article.order(:last_activity_date => 'DESC') 然后,当您收到文章时,请输入Article.order(:last_activity_date => 'DESC')

EDIT: based on your clarifications - 编辑:根据您的说明-

# Merge arrays of article objects
all_articles = current_blog_articles | reblogged_articles 
all_articles.sort_by! {|x| -x.created_at } # Sort by created_at descending

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM