PG ::错误:错误:列tutorials.tutorialcategory_id不存在

时间:2015-11-18 08:15:34

标签: ruby-on-rails ruby-on-rails-3 associations

我有两个型号'教程'和' Tutorialcategory'

class Tutorialcategory < ActiveRecord::Base
has_many :tutorials


class Tutorial < ActiveRecord::Base
    belongs_to :tutorialcategory

教程与多个类别相关联,例如html,rubyonrails,其中html和ruby on rails是tutorialcategories

以下是迁移

class CreateTutorials < ActiveRecord::Migration
  def change
    create_table :tutorials,force: true do |t|
      t.string :title
      t.text :body
      t.integer :rating
      t.string :videoid
      t.belongs_to :tutorialcategory

      t.timestamps
    end
  end
end


class CreateTutorialcategories < ActiveRecord::Migration
  def change
    create_table :tutorialcategories do |t|
      t.string :title

      t.timestamps null:false
    end
  end
end

所有教程都在索引页面上正确列出,但是当我看到类别页面时,它会给我以下错误

PG::Error: ERROR:  column tutorials.tutorialcategory_id does not exist

1 个答案:

答案 0 :(得分:0)

我不知道您为什么将模型Tutorialcategory命名为TutorialCategory而不是rake db:rollback ,它们遵循Rails命名约定并使其更容易理解。

首先,回滚db一步

class CreateTutorials < ActiveRecord::Migration
  def change
    create_table :tutorials,force: true do |t|
      t.string :title
      t.text :body
      t.integer :rating
      t.string :videoid
      t.belongs_to :tutorial_category, index: true

      t.timestamps
    end
  end
end


class CreateTutorialCategories < ActiveRecord::Migration
  def change
    create_table :tutorial_categories do |t|
      t.string :title

      t.timestamps null:false
    end
  end
end

将您的迁移文件更改为:

class TutorialCategory < ActiveRecord::Base
  has_many :tutorials


class Tutorial < ActiveRecord::Base
  belongs_to :tutorial_category

再次运行迁移并编辑模型以匹配新架构。

def func(x,*args):
    t = 0
    x[0] += args[0]
    x[1] += args[1]