Permalink
Browse files

Add relationships between Users and Tweets

  • Loading branch information...
twata701 committed Feb 19, 2014
1 parent 9065e2f commit eb94cbc5ef7f39cb25d5024abacb3beb688e028c
@@ -7,7 +7,8 @@ class TweetsController < ApplicationController
# GET /tweets
# GET /tweets.json
def index
@tweets = Tweet.order(:id).page params[:page]
@user = current_user
@tweets = @user.tweet.order(:id).page params[:page]
end
# GET /tweets/1
@@ -71,7 +72,7 @@ def import_csv_new
# POST /tweets/import_csv
def import_csv
respond_to do |format|
if Tweet.import_csv(params[:csv_file])
if Tweet.import_csv(params[:csv_file],current_user)
format.html { redirect_to tweets_path }
format.json { head :no_content }
else
View
@@ -4,12 +4,14 @@
class Tweet < ActiveRecord::Base
def self.import_csv(csv_file)
belongs_to :user
def self.import_csv(csv_file,user)
# csvファイルを受け取って文字列にする
csv_text = csv_file.read
# 読み込む前に古いツイートをすべて削除する。
Tweet.delete_all
user.tweet.delete_all
#文字列をUTF-8に変換
CSV.parse(Kconv.toutf8(csv_text)) do |row|
@@ -28,7 +30,8 @@ def self.import_csv(csv_file)
tweet.retweeted_status_user_id = row[7]
tweet.retweeted_status_timestamp = row[8]
tweet.expanded_urls = row[9]
tweet.user_id = user.id
tweet.save
end
View
@@ -3,6 +3,8 @@ class User < ActiveRecord::Base
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :trackable, :omniauthable
has_many :tweet, dependent: :destroy
def self.find_for_twitter_oauth(auth)
user = User.where(:provider => auth.provider, :uid => auth.uid).first
unless user
@@ -0,0 +1,5 @@
class AddUserIdToTweets < ActiveRecord::Migration
def change
add_column :tweets, :user_id, :integer
end
end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140217151640) do
ActiveRecord::Schema.define(version: 20140219141653) do
create_table "tweets", force: true do |t|
t.string "tweet_id"
@@ -26,6 +26,7 @@
t.string "expanded_urls"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
create_table "users", force: true do |t|

0 comments on commit eb94cbc

Please sign in to comment.