RailsCasts Pro episodes are now free!
Learn more or hide this
Great value as usual, just one question Ryan.
Do you dislike find_or_create (which I could understand BTW) or
maybe think that exists? ... || create! has other merits (like the bangs exception raising behavior).
exists? ... || create!
I'd been wrestling with trying to cache my 'latest tweet' partial and the cron job not expiring it properly for a while, this is a much tidier way to do it, with no delay in page loads.
Just one question: I saw in the end that it's possible to change app permission dinamically, is it possible to change app name too? It would be useful for I18n, I have an app that has a name associated to an idiom and another name for another idiom.
cache can take an array, so you should do cache [testimonies, Date.today], no?
cache [testimonies, Date.today]
I've actually been having troubles storing the tweets in the database. With using postgresql, I get this error:
: SELECT 1 AS one FROM "tweets" WHERE "tweets"."tweet_id" = 222042501954019328 LIMIT 1
ActiveRecord::StatementInvalid: PGError: ERROR: operator does not exist: character varying = bigint
I had to actually declare the table creation differently.
class CreateTweets < ActiveRecord::Migration
create_table :tweets, :id => false do |t|
t.integer :tweet_id, :limit => 8
per this article: http://moeffju.net/blog/using-bigint-columns-in-rails-migrations
Not sure if this is the best method - but currently working on my end!
This was very helpful for me. Thanks for sharing.
Thanks for posting, very useful.
Although you shouldn't need the id => false in the create_table declaration. The example given from Matt Bauer from what I can see is using the id as the bigint column whereas here we have replaced it with tweet_id. I have tested this myself and I can confirm it works.
class CreateLatestTweets < ActiveRecord::Migration
create_table :latest_tweets do |t|
t.integer :tweet_id, :limit => 8
Super helpful. Thanks!
Can someone fill in the gap that occurs around 8:40? I'm using the latest version of the Twitter gem, and when I set up a twitter.rb initializer as specified, and configure environment variables to match up with the lines in the twitter.rb, go into the rails console, and run Twitter.favorites(count: 1) to test things, I get:
Twitter::Error::Unauthorized: This method required authentication...
Did something change in the Gem? Or, are there steps missing from the railscast?
I've basically followed the directions exactly, with the exception that the Gem I use is the latest Twitter Gem, located at github.com/sferik/twitter.
Also having this problem...Unsure as to how to treat that initializer file.
Did you find the solution of this issue?
I'm having this same issue. Have these steps changed with the new Twitter API updates?
Please refer to the new changes in sferik's gem: https://github.com/sferik/twitter#configuration
Is anyone having a problem displaying the profile_image_url? My code is below. The only thing that will display is the alt text of the img, never the image itself. However, when I review HTML, it has a url in the img and if I hover over the link it displays the image itself. This tells me the path is correct.
Any suggestions would be greatly appreciated.
<% if Twitter.rate_limit_status.remaining_hits > 1 %>
<% Twitter.search(Channel.find(params[:id]).channel_name, :lang => "en", :rpp => 4, :result_type => "recent").each do |tweet| %>
<%=image_tag(tweet.profile_image_url, :size => "16x10") %>
<blockqoute><%= tweet.text %></blockqoute>
<% end %>
<% end %>
The profile image is tweet.user.profile_image_url
I may be confused on the Twitter Secret & Consumer keys... but should these values be stored in a hash?
In the unfortunate event of a database being hacked - wouldn't that give access to post on user's Twitter accounts?
The idea of the keys is to limit the access only to the API. So in that case you can still log in to Twitter and reset your application keys.
Don't make the mistake of setting tweet_id's type to an integer. MySQL maxes out at 2147483647 unless you specify a limit. It's easier to just make it a string.
How is it possible to extract hashtags via the twitter gem? I tried with:
@hashtags ||= entities(Twitter::Entity::Hashtag, :hashtags)
But it is on the view site I get in trouble! :s
How do I do the same for a authorized user, I've got the following in my controller, model, and index;
@bins = Bins.new
@bins = Bins.all
@tw = Newtweets.order("tweet_id desc").limit(100)
twitter.home_timeline(since_id: maximum(:tweet_id)).each do |tweet|
unless exists?(tweet_id: tweet.id)
<% @tw.each do |t| %>
<div class="selection"><%= check_box_tag :label %></div>
<div class="stamp unknown"><%= t.time_date %><br /><span class="date"><%= date.day %>/<%= date.mon %>/<%= date.year %></span></div>
<p><%= t.content %>
<br /><span class="username"><a href="https://www.twitter.com/<%= t.screen_name %>" target="_blank">@<%= t.screen_name %></a></span><span class="followers">Followers:<%= t.followers %></span><span class="retweets">Retweets: <%= t.retweets %></span></p>
<hr style="width:93%;height:1px;margin:-5px auto 10px auto;border:none;border-bottom:1px solid #EDEDED;" />
<% end %>
As you've guessed it, I'm trying to use a refresh button to pull the route /refresh which runs the controller, that runs the model??
Not sure I'm doing this right. However, going in the console with the following command;
I get an error: NameError: uninitialized constant User::Newtweet
Should I be moving the self.pull_tweets stuff into a controller??
Any suggestions, thank you!
If you want to use Twitter authentication in https://teatro.io/ first create special stage application in Twitter and then set domain as project-token.ttrcloud.com and authentication urls as http://project-token.ttrcloud.com. It will work on any branch of this project.
If at 2:45 when Ryan interacts with Twitter methods in the console you receive an "Unable to verify your credentials" error, see this Stack Overflow thread:
Steps involve copying and pasting the Twitter.configure block mentioned in the thread into omniauth.rb, then supplying config.oauth_token and config.oauth_token_secret with values from dev.twitter.com.
This is done by generating access tokens. I did this by visiting the Twitter app I created, clicking on the "API Keys" tab, then generating the access tokens at the bottom of the page. Doing so will supply you with values that you can plug into the Twitter.configure block mentioned above.
One thing that isn't clear is this: Does a User have to authenticate with Twitter (through, like OmniAuth), or can they just authorize an app with read | read/write access?