0

Twitter API を操作して、Web サイトに user_timeline を表示しようとしています。

Twitter 統合については railscasts.com のビデオをフォローしました: http://railscasts.com/episodes/359-twitter-integration

私は API を問題なく操作しており、アプリケーションに情報を取り込み、表示して開発を行っています。

私のコードは次のとおりです。

モデル-timeline.rb

class Timeline < ActiveRecord::Base
  attr_accessible :content, :screen_name, :tweet_id

  def self.pull_tweets
   Twitter.user_timeline("{username_goes_here}", since_id: maximum(:tweet_id)).each do |tweet|
    unless exists?(tweet_id: tweet.id)
      create!(
        tweet_id: tweet.id,
        content: tweet.text,
        screen_name: tweet.user.screen_name,
      )
    end
  end
end
end

移行は次のとおりです。

class CreateTimelines < ActiveRecord::Migration
  def change
    create_table :timelines do |t|
     t.string :tweet_id
     t.string :screen_name
     t.text :content

     t.timestamps
end

終了 終了

ツイートを表示するには:

<div id="timeline">
      <% Timeline.order("tweet_id desc").limit(3).each do |timeline| %>
         <h3><%= timeline.content %></h3>

        <p class="pull-right">
            ~ @<%= timeline.screen_name %>
        </p>
      <% end %>
    </div>

アイデアは、ツイートをデータベースに保存して、Twitter がダウンした場合でも、最新のツイートを表示しているユーザーに影響を与えないようにすることです。

とにかく、Timeline.pull_tweetsコンソールでコマンドを実行すると、正常に動作します。

Heroku にプッシュし、データベースを移行して、同じコマンドを実行しようとしたときです。

次に、次のエラーが表示されます。

  PGError: ERROR:  operator does not exist: character varying = bigint
LINE 1: ...ne FROM "timelines"  WHERE "timelines"."tweet_id" = 21919081...
                                                         ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

何が起こっているのですか?

また、整数であるように移行を実行しようとしました:tweet_idが、herokuでも別のエラーが発生しました。

ありがとう。

4

1 に答える 1

2

tweet_id文字列として作成しました ( varchar(255)PostgreSQL では別名):

create_table :timelines do |t|
  t.string :tweet_id

しかし、あなたのtweet.id

unless exists?(tweet_id: tweet.id)

実は数字です。あなたを文字列として保存し続けたい場合は、それを使用するすべての場所で文字列tweet_idに変換する必要があります。id

unless exists?(tweet_id: tweet.id.to_s)
  create!(
    tweet_id: tweet.id.to_s,
    ...

tweet_id現在の文字列の代わりに整数を使用するように本番テーブルを修正する場合は、いくつかのオプションがあります。

  1. テーブルを削除して、正しいスキーマで再作成してください。これは問題なく動作しますが、持っているデータはすべて失われます。
  2. 手動でALTER TABLE を発行して、USINGを使用して PostgreSQL に文字列を整数に変換する方法を指示できるようにします。

それを理解したら、Heroku へのデプロイを計画している場合は、PostgreSQL をローカルにインストールし、PostgreSQL の上で開発する必要があります。

于 2012-07-02T00:25:26.180 に答える