ActionView :: Template :: Error(PG :: UndefinedColumn:ERROR:列“user_id”不存在

时间:2015-03-10 09:04:29

标签: ruby-on-rails postgresql heroku

我在heroku上部署了我的项目。

crackeryourwardrobe.heroku.com是我的网站。

当我点击facebook登录时,我在heroku日志中收到此错误

PG::UndefinedColumn: ERROR:  column "user_id" does not exist
SELECT "products".* FROM "products"  WHERE (user_id = 1)  

my view.rb

<% if session[:user_id] %>
  <% @account_products.order(created_at: :desc).in_groups_of(3) do |product_group| %>
    <div class = "row">
      <% product_group.compact.each do |product| %>
        <div class="col-md-4">

products.rb     类产品&lt;的ActiveRecord :: Base的         has_many:line_items,dependent :: destroy         has_many:wardrobe_item,dependent :: destroy         mount_uploader:item,ItemUploader     端

wardrobe_item.rb     class WardrobeItem&lt;的ActiveRecord :: Base的         belongs_to:产品         belongs_to:用户     端

user.rb

class User < ActiveRecord::Base
    has_many :wardrobe_items, dependent: :destroy

的database.yml

default: &default
  adapter: sqlite3
  pool: 5
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

schema.rb

  create_table "products", force: true do |t|
    t.string   "title"
    t.text     "brand"
    t.string   "image_url"
    t.decimal  "price"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "item"
    t.integer  "user_id",    limit: 255

product.rb

class Product < ActiveRecord::Base
    has_many :line_items, dependent: :destroy
    has_many :wardrobe_item, dependent: :destroy
    mount_uploader :item, ItemUploader 

end

wardrobe_item.rb

class WardrobeItem < ActiveRecord::Base
    belongs_to :product
    belongs_to :user
end

schema.rb

ActiveRecord::Schema.define(version: 20150310085128) do

  create_table "line_items", force: true do |t|
    t.integer  "product_id"
    t.integer  "cart_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "quantity",   default: 1
  end

  create_table "products", force: true do |t|
    t.string   "title"
    t.text     "brand"
    t.string   "image_url"
    t.decimal  "price"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "item"
    t.integer  "user_id",    limit: 255
  end

  create_table "stores", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "provider"
    t.string   "uid"
    t.string   "name"
    t.string   "oauth_token"
    t.datetime "oauth_expires_at"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "cart_id"
    t.integer  "wardrobe_item_id"
  end

  create_table "wardrobe_items", force: true do |t|
    t.integer  "product_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"

我不知道如何解决它......帮助.....请...

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为用户模型和产品型号没有完全连接。

在用户模型中,添加此代码

user.rb

class User < ActiveRecord::Base
    has_many :wardrobe_items, dependent: :destroy
    has_many :products, through: :wardrobe_items # this is code

它会通过用户连接到产品:)