2

&nbsp I am currently implementing a web application similar to Twitter. In my Twitter application I have an action 'retweet' which obviously is also in the real twitter.

How I thought of implementing it ?

  I will have a model with the following columns:

  • 1. retweeted_tweet_id(which tweet was retweeted)
  • 2. who_user_id(who retweeted it)
  • 3. retweeted_at(time when it was retweeted)

&nbsp All good and well. When I will have to return all tweets I will just have to use the '+' to concatenate 2 arrays(one that contains tweets and one that contains retweets). This action will show the tweets in chronological order but I won't be able to add "retweeted by username" unless I query again the Retweets model by tweet_id and retweeted_at time(the time of the tweet != retweeted_at_time) to actually confirm that that tweet is a retweet. After corfirming it's a retweet I add the "This post was retweed by username

&nbsp This brings me to my questions: Is there a simpler way to do this ? I can't think of any other way using this model. Is there a more efficient model ?

Lucian Tarna
  • 1,806
  • 4
  • 25
  • 48
  • Maybe it makes sense to think of other approach on general architecture? If tweets and retweets will be in the same table it will make your life much easier. Thoughts? – Mikhail Nikalyukin Feb 12 '15 at 10:26
  • yes they would but I've read a post on stackoverflow about this http://stackoverflow.com/questions/11654420/how-to-implement-twitter-retweet-action-in-my-database and the general consensus was that this option is better. Well I will need to add also another row in my model as I want to be able to add some of my own text if I retweet a tweet :D – Lucian Tarna Feb 12 '15 at 10:27
  • I see, if you take more carefully look to the accepted answer on the question you mention they propose to use JOIN or INCLUDE. That way you will have active record collection, not 2 arrays that you need to concatenate. Therefore you can fetch retweeted by username without fetching database twice. Does it makes sense? – Mikhail Nikalyukin Feb 12 '15 at 10:30
  • I think you are referring to pulling out all the tweets and retweets for a certain user ? If that is so , that's not why I am asking what I am. The thing I am having doubts about is that I need to add a special html element let's say a

    and in it to write retweeted by username but this only for retweets . How do I verify if a tweet is the original tweet or a retweet without querying after tweet_id and created_at ? . A must is ordering all the tweets and retweets that are show in the page in chronological order. (this is what their query does)

    – Lucian Tarna Feb 12 '15 at 10:39
  • http://chat.stackoverflow.com/rooms/70778/rails-retweet let's go for chat – Mikhail Nikalyukin Feb 12 '15 at 10:47

1 Answers1

1

You need to use .joins or .include to fetch your retweets, that way in the end you will have ActiveRecord collection, not Array.

That means that you can get retweeted_by_username without fetching your database again, since record already loaded.

Our you can go other way and choose to store tweets and retweets in the same table with field called original_tweet_id, that way you will solve your current problem, plus it would be easier for you to add any text to the retweet, you can store it in the record as tweet text. Later you are just checking for original_tweet_id to verify if this is a retweet or no.

Mikhail Nikalyukin
  • 11,867
  • 1
  • 46
  • 70
  • I will go for original_tweet_id field , it seems to solve many of the problems I had with many queries in the database. Thank you for all your help! – Lucian Tarna Feb 12 '15 at 11:11