How do you run the "Rails Runner" in the hero?
here's what I'm trying to do: find if there is anyone on twitter about a particular course. If someone actually tweeted about this, I would like to save this tweet in my Tweet model and then display that tweet on the appropriate course page.
The scripts work locally by running rails runner get_tweets.rb
, but on Heroku it seems like the script is being executed but not written to the database. In heroku, I run 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 am getting 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"
source to share
As seen from your mistake
"186306985577299969" value out of range for integer type
you need to use a different datatype (for tweet_id
I believe), presumably BIGINT
which ranges from -9223372036854775808 to 9223372036854775807.
To do this in Rails, you can pass :limit => 8
in your migration up
:
change_column :tweets, :tweet_id, :integer, :limit => 8
Please note that you should always make some kind of notes or reports when you rescue
, or errors like this, become very difficult to track down because they are silently bypassed.
source to share
The Twitter API actually sends back two types of id for tweets and users and so on, one is a number, where the other is a string representation of a number. This is because it is a very real possibility that they will have so many tweets and users that the ID can overflow in different implementations. If you use String as value when inserting it shouldn't happen anymore.
The field is called id_str
source to share