首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActiveRecord::StatementInvalid in PicController#favorite

ActiveRecord::StatementInvalid in PicController#favorite
EN

Stack Overflow用户
提问于 2015-05-05 06:19:42
回答 1查看 61关注 0票数 1

当我尝试从使用Twitter API的推文列表中收藏一张图片时,我的Rails应用程序中出现了一个错误。

这是pic_controller.rb

代码语言:javascript
复制
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

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2015-05-05 13:33:08

我认为您应该首先为这个运行"rails g migration create_(表的名称)“创建一个迁移表。然后在执行之后,在终端"rake db:migrate“中运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30041001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档