1

I have the following table:

Tweet(id: integer, from_user: string, tweet_text: string, tweet_id: integer, course_id: integer, tweet_already_exists: boolean, tweet_posted_to_reviews: boolean, created_at: datetime, updated_at: datetime) 

And, for a specific course, I'd like to get the last 50 tweets, unique by user. I've gotten this far:

 Tweet.order("tweet_id desc").find_all_by_course_id(@course.id).first(50)

The problem with this is that if a user has tweeted more than once, they appear twice (both tweets appear) and I'd only like to get that person's latest tweet.

Thank you!

2 Answers 2

1

It's a bit complex, but I'm not sure it's possible to do the query you want to do in a simpler way:

Tweet.where("tweets.updated_at >= (SELECT MAX(t.updated_at) FROM tweets AS t WHERE t.from_user=tweets.from_user)").order("tweets.tweet_id desc").find_all_by_course_id(@course.id).first(50)
5
  • Hi tsherif, this is brilliant! It works almost perfectly! The only thing is that it is pulling the user's first tweet (instead of the last one). When I change .order("tweets.tweet_id desc") to .order("tweets.tweet_id asc") it only changes the ordering of the tweets on the page (v.s. pulling the user's latest tweet). any idea on how to fix that? also, do you happen to know if this query will work on PG (site is on heroku using Cedar stack)? Thanks again for your help, much appreciated! Apr 11, 2012 at 1:19
  • Try changing the >= to <= and the MAX in the nested query to MIN. Not sure about the PG stuff though...
    – tsherif
    Apr 11, 2012 at 1:25
  • Brilliant! Thank you so much tsherif, it works locally. Please let me know if there are any specific resources you recommend I look at to understand how you built the query. Thanks again!! Apr 11, 2012 at 1:29
  • No problem. Glad it helped :) I think if you look up nested queries in Google, you should find some info on how these kinds of queries are build. Good luck!
    – tsherif
    Apr 11, 2012 at 1:33
  • BTW, you could also try by MAX(tweets.created_at). It sort of depends how your table is organized.
    – tsherif
    Apr 11, 2012 at 1:38
0

I haven't tested it, but try this :

Tweet.find(:all, :select => 'DISTINCT from_user', :order => 'tweet_id desc')

Will give you the list of tweets without duplicated users. Query the result to get the courses.


You night also want to use the ruby method uniq which return an array without duplicate.

6
  • I've tried that but can't seem to make it work inside with all other parameters. Where would you add the uniq call (to find unique "from_user")? Apr 11, 2012 at 0:33
  • You only have one table for everytthing (users, courses, tweets)?
    – Justin D.
    Apr 11, 2012 at 0:36
  • In this case I have the Tweet table (with the fields outlined above - including the from_user) and a Course table, which has course_id, and other fields related to the courses. Thanks again for your help. Apr 11, 2012 at 0:40
  • Thanks for your help Justin. This is giving me an array, not sure if there is a way to do this directly on active record. Basically, is there a way to add "find_all_by_course_id" to the query you posted? Apr 11, 2012 at 0:57
  • Try adding .find_all_by_course_id(your code) after the query I gave you. Not sure if it is going to work, but it is worth a try.
    – Justin D.
    Apr 11, 2012 at 1:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.