我尝试与用户表创建注释关联,但是当我转到rails控制台并尝试查看它是否有效时,它会给我以下错误:
user = User.first
user.comments
undefined method `comments'
以下是我正在使用的代码。
class CreateComments < ActiveRecord::Migration[5.1]
def change
create_table :comments do |t|
t.text :description
t.references :user, index: true, foreign_key: true
t.timestamps
end
end
end
class CreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
t.string :username
t.string :email
t.timestamps
end
end
end
class Comment < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_many :comments
end
我正在使用最新版本的ruby。
谢谢!
答案 0 :(得分:0)
我没有看到任何未定义方法错误的原因, 您可能正在等待迁移 运行此命令
rails db:migrate