我有两个模型'Tutorial‘和'Tutorialcategory’
class Tutorialcategory < ActiveRecord::Base
has_many :tutorials
class Tutorial < ActiveRecord::Base
belongs_to :tutorialcategory教程与多个类别相关联,如html、rubyonrails,其中html和ruby on rails是教程类别。
以下是迁移
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发布于 2015-11-18 17:03:13
我不知道为什么您将模型命名为Tutorialcategory而不是TutorialCategory,因为它遵循Rails命名约定,并且更容易理解。
首先,回滚数据库一步。
rake db:rollback将迁移文件更改为:
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_categoryhttps://stackoverflow.com/questions/33774798
复制相似问题