0

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. 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.

In my blogs show action I am then adding blog.articles with blog.reblogged_articles to one single array of articles. I then sort this array by the created_at attribute of each element.

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. 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.

I probably need to modify the created_at attributes of my reblogged_articles array before I put the two arrays together. But how do I do that?

Edit: Just an example to clarify:

Blog id 1:
Article id 1, created_at: A year ago.
Article id 2, created_at: A month ago.
Article id 3, created_at: A week ago.
Article id 4, created_at: A day ago.

Blog id 2:
Article id 5, created_at: A day ago.
Article id 6, created_at: An hour ago.
Article id 7, created_at: 30 minutes ago.
Article id 8, created_at: About 10 minutes ago.

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. 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?

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?

rails_has_elegance
  • 1,590
  • 4
  • 21
  • 37

1 Answers1

0

The easiest solution is probably just adding a last_activity_date column to your article model. Whenever an article is reblogged, update the corresponding article's last_activity_date. Then, when you're getting articles, 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
agmin
  • 8,948
  • 2
  • 21
  • 27
  • thank you for your answer! I like the approach, however, I still wouldn't be able to sort my mixed array of blog.articles and blog.reblogged_articles how I want, since with the last_activity the articles of my own blog would be mixed up based on reblogs of others, or am I understanding your approach wrong? – rails_has_elegance Oct 24 '12 at 23:05
  • I think I didn't word my question really well by saying "But currently, a reblogged article might appear on the bottom of the blog, if the article was created 3 years ago." What I mean, is the perspective of the blog user, who just has reblogged some other article for his own blog. That newly reblogged article consequently should appear on the top of that reblogging user's blog, even though it might be created 3 years ago in the origin blog. – rails_has_elegance Oct 24 '12 at 23:07