Rails newbie. I'm writing an application that pulls a Twitter user timeline using the sferik Twitter gem, iterates through the timeline and writes each individual tweet into a model/table. I can get the user timeline into a hash but when I try and iterate (e.g. tweet.each do..) and save each tweet the application only saves the last tweet from the hash, not each individual tweet. I have tried to do a nested loop but get an undefined "each" method on the second level iteration. Code below, all suggestions welcome.
Rails 3, PostgreSQL, Twitter gem.
**Controller**
**class TweetController < ApplicationController**
require 'twitter'
**def tweet**
@tweet = Tweet.new
@tweeters = User.tweeters.all #get twitter users in my application
client = Twitter::REST::Client.new do |config|
config.consumer_key = secret
config.consumer_secret = secret
config.access_token = secret
config.access_token_secret = secret
end
@tweeters.each do |element| *#for each user get tweets to insert into the model*
@tweet.user_id = element.id
@tweet.screen_name = element.screen_name
@content = client.user_timeline(element.twitter_id, :since_id => element.tweet_since_id).reverse
@content.each do |post| *#if I return 4 twitter posts this loop only inserts the last post*
if post.id > element.tweet_since_id
element.tweet_since_id = post.id
@tweet.content = post.text
@tweet.save
end
end
end
end
So the "@content.each do |post|" section does not go through each tweet and insert 4 different records, it only inserts the last record. I have tried every form of the following and receive an "undefined each method for Twitter id..."
@content.each do |post|
post.id.each do |t|
if t.id > element.tweet_since_id
element.tweet_since_id = t.id
@tweet.content = t.text
@tweet.save
end
end
Any thoughts or suggestions?