如何增加 Rake 脚本中的值?
如何更改此 :project_pages_id => 1
值自动递增?
user.projects.create!(:title => Faker::Lorem.sentence(1), :project_pages_id => 1)
How can I change this :project_pages_id => 1
value to auto increment?
user.projects.create!(:title => Faker::Lorem.sentence(1), :project_pages_id => 1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要迭代一个数组,例如:
我确信还有其他方法,但这又快又脏,而且只是一个测试。
You'd need to iterate over an array like:
I'm sure there is other ways, but this is quick and dirty, and it's only a test.
该project_pages_id是否打算作为外键?如果是这样,为什么要自动递增它以使其具有零关联?
您似乎正在尝试创建种子数据。一个好方法是使用 Factory Girl:
https://github.com/thoughtbot/factory_girl
除其他外,它具有“序列”的概念,它解决了您原来的问题:
Is that project_pages_id intended to be a foreign key? If so, why would you auto-increment it such that it will have a nil association?
It looks like you're trying to create seed data. A good way to do that is to use Factory Girl:
https://github.com/thoughtbot/factory_girl
Among other things, it has the concept of "sequences", which solves your original question: