您好,我是 Ruby on Rails 的新手。我正在尝试创建一个小型博客站点。我有两个表,即“帖子”和“评论”。每篇帖子都会有很多评论。我使用以下命令生成这些表格:
rails g scaffold Post title:string body:text author:string
rails g scaffold Comment body:string author:string
现在我想将关系添加到模型类中。我在Post类中添加了has_many :comments
,并在Comment类中添加了belongs_to :post
。但是,当我尝试调用post.comments
时,我遇到了一个运行时错误,说SQLException: no such column: comments.post_id
。我应该创建一个迁移并在Comment下添加post_id,还是在脚手架建立时就有一种方法可以实现这一点?