2015-05-04 223 views
1

當我嘗試從使用Twitter API的推文列表中收藏圖片時,我在我的Rails應用程序中出現錯誤。ActiveRecord :: StatementInvalid在PicController#收藏夾

這裏的pic_controller.rb

class PicController < ApplicationController 
    def favorite 
     if current_user.present? 
      pic = Pic.find(params[:url]) 
      FavPic.create pic: pic, user: current_user 
      # user and pic automaically have this `FavPic` assigned 
     end 
    end 
end 

和這裏的user.rb

class User < ActiveRecord::Base 
    def self.from_omniauth(auth) 
    user = where(provider: auth.provider, uid: auth.uid).first || create_from_omniauth(auth) 
    user.oauth_token = auth["credentials"]["token"] 
    user.oauth_secret = auth["credentials"]["secret"] 
    user.save! 
    user 
    end 

    def self.create_from_omniauth(auth) 
    create! do |user| 
     user.provider = auth["provider"] 
     user.uid = auth["uid"] 
     user.name = auth["info"]["nickname"] 
    end 
    end 

    def twitter 
    if provider == "twitter" 
     @twitter ||= Twitter::Client.new(oauth_token: oauth_token, oauth_token_secret: oauth_secret) 
    end 
    end 

    has_many :fav_pics 
    has_many :pics_favorited, 
    class_name: 'Pic', 
    through: :fav_pics 

end 

class FavPic < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :pic 
end 

class Pic < ActiveRecord::Base 
    has_many :fav_pics 
    has_many :fav_users, 
    class_name: 'User', 
    through: :fav_pics 
end 

不知道我要去的地方錯或如何排查但是這是我得到的錯誤。 PG::UndefinedTable: ERROR: relation "pics" does not exist LINE 5: WHERE a.attrelid = '"pics"'::regclass^: SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum WHERE a.attrelid = '"pics"'::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum

+1

你運行過'rake db:migrate'嗎?假設你有一個遷移來創建'pics'表。 – DiegoSalazar

+0

@ diego.greyrobot我如何創建這樣的遷移?我想這就是我所缺少的,因爲我運行了'rake db:migrate',因爲我已經通過 – sdybskiy

+0

查看了很多q的建議show your'schema.rb'文件 –

回答

0

我認爲你應該首先爲這個運行創建一個遷移表「rails g migration create_(table of name)」。然後執行後,在終端「rake db:migrate」中運行。

相關問題