开发者社区> 问答> 正文

如何在Ruby on Rails种子中关联用户外键?

我正在创建一个新的rails应用程序,我想添加一些种子数据。该应用程序将显示属于用户的不同发型,这意味着每个发型需要user_id。在我的种子文件中,我创建了用户和发型但是当我在控制台中运行rails:dbseed时它给了我错误:

"NameError: undefined local variable or method `user' for main:Object
Did you mean? users"
当我运行rails c时,我看到用户已创建,但我的hairstyles没有。
种子文件:

Hairstyle.destroy_all

User.destroy_all

users = [
{

first_name: 'test',
last_name: 'Inniss',
email: 'angela@hair.com',
password: '123456' 

},
{

first_name: 'test2',
last_name: 'Clarke',
email: 'aaron@hair.com',
password: '123456'

}
]

User.create!(users)

user.save

hairstyles = [
{

name: 'Single Braids',
description: 'Really easy to maintain',
category: 'Braids',
video_url: 'www.google.com',
photo_url: 'https://images.unsplash.com/photo-1533233336213-b3a32825c689?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1049&q=80',
user_id: User.first

},
{

name: 'Crochets',
description: 'Protective style',
category: 'Braids',
video_url: 'www.google.com',
photo_url: 'https://images.unsplash.com/photo-1533233336213-b3a32825c689?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1049&q=80',
user_id: User.first

}
]

Hairstyle.create!(hairstyles)

展开
收起
小六码奴 2019-04-10 15:55:34 2533 0
1 条回答
写回答
取消 提交回答
  • 只需删除此行即可

    user.save
    它没有意义,你不需要它。

    2019-07-17 23:33:07
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载