here is what I am trying to do: Find if anyone has tweeted about a specific course offered. If someone has indeed tweeted about it, I’d like to save that tweet to my Tweet Model and then display that tweet in the corresponding course page.
The scripts works locally by running rails runner get_tweets.rb but on Heroku it seems that the script gets executed but doesn’t write to the database. In heroku I am running heroku run rails runner get_tweets.rb (using the Cedar stack).
def get_course_tweets
@courses = Course.all
@courses.each do |course|
url = course.url
tweets = Twitter.search(url, {:rpp => 100, :recent => true, :show_user => true})
tweets.each do |tweet_info|
unless Tweet.find_by_tweet_id(tweet_info.id).present?
tweet = Tweet.new
tweet.course_id = course.id
tweet.tweet_id = tweet_info.id
tweet.tweet_text = tweet_info.text
tweet.from_user = tweet_info.from_user
begin
tweet.save!
rescue => error
puts error
end
end
end
end
end
Edit:
The current error I get from rescue is the following:
PG::Error: ERROR: value "186306985577299969" is out of range for type integer : INSERT INTO "tweets" ("book_id", "course_id", "created_at", "from_user", "tutorial_id", "tweet_already_exists", "tweet_id", "tweet_posted_to_reviews", "tweet_text", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"
As can be seen from your error
you need to use a different datatype (for
tweet_id, I believe), presumably aBIGINT, which ranges from -9223372036854775808 to 9223372036854775807.To do so in Rails you can pass
:limit => 8in yourupmigration:Note that you should always do some sort of logging or reporting when you
rescue, or else bugs like this become very difficult to track down because they silently get bypassed.