Rails 种子混乱
我在使用 seed.rb
为数据库播种时遇到问题,特别是在涉及表关系的情况下。
这是代码示例:
# seed.rb
user = User.find_or_create_by_login(
:login => "myname",
:email => "[email protected]",
:user_type => "Admin",
:password => "admin",
:password_confirmation => "admin")
project = Project.find_or_create_by_user_id(
:user_id => user.id,
:name => "Test Project")
创建项目时(以及我在上面省略的其他不相关参数),user_id 为空。我怎样才能让它发挥作用?
这是我在如此简单的事情中见过的最奇怪的行为。在我的种子文件中,我创建了大约八个表,其中一些表嵌套了 3-4 层(即用户 has_many 项目;项目 has_many 任务等)。
当我如上所述调用 user user
并多次引用 user.id
时,它只能工作一次!我尝试在创建每个新记录之前添加 [user.reload] 但无济于事。我不认为这对任何人都有意义,但是这里有任何可能性吗?谢谢大家。
I'm having trouble seeding my database using seed.rb
, specifically where table relationships are concerned.
Here's a sample of the code:
# seed.rb
user = User.find_or_create_by_login(
:login => "myname",
:email => "[email protected]",
:user_type => "Admin",
:password => "admin",
:password_confirmation => "admin")
project = Project.find_or_create_by_user_id(
:user_id => user.id,
:name => "Test Project")
When project is created (along with other unrelated parameters I've left out from above), user_id is empty. How can I get this to work?
This is the strangest behavior I've seen in something so simple. In my seed file, I have about eight tables being created and some are nested 3-4 levels deep (i.e. user has_many projects; projects has_many tasks, etc.).
When I call user user
as above and reference user.id
multiple times after that, it only works once! I tried adding [user.reload] before each new record is created but to no avail. I don't imagine this will make sense to anyone, but are there any possibilities here? Thanks all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我明白问题出在哪里了。未填充的字段未在其各自模型的
attr_accessible
中明确列出。列出的字段已正确保存。非常感谢大家的帮助。
I figured out what the problem was. The fields that weren't populating were not listed explicitly in
attr_accessible
in their respective models. The fields listed were being saved correctly.Thank you very much for your help everyone.
该代码很好,并且是
find_or_create
的正确语法。正如其他人所说,最有可能的问题是用户无效。如果用户无效,尝试调用 user.reload 会使其崩溃,因此会使问题更加明显,但从中得到的错误将是无用的(它会抱怨无法找到没有 id 的用户)。不幸的是,如果 find_or_create 无效,则它不能作为引发异常的 bang 方法,因此最好的办法可能是引发错误并在尝试创建用户后输出错误:
The code is fine and is the correct syntax for
find_or_create
. As others have said the most likely problem is that the user is invalid. Trying to calluser.reload
would make it blow up if the user is invalid and so will kind of make the problem more apparent, but the error you'll get from it will be useless (it'll moan about not being able to find a user without an id).Unfortunately
find_or_create
doesn't work as a bang method to raise exceptions if it's invalid, so the best thing to do is probably raising an error and outputting the error after attempting to create the user:用户创建成功?如果是这样..尝试 user.reload 如果没有。这可能是错误
User created with success? if so..try user.reload if not. that is probably the error
您确定您的用户已保存吗?我认为
find_or_create_by_XX
的正确语法是Blog.find_or_create_by_title("Some Blog")
。如果您需要传递更多数据,则需要先使用find_or_initialize,然后分别设置其他数据。松散相关的线程:Rails find_or_create 通过多个属性?
--edit
将数据作为散列传递给
find_or_create_by_XX
似乎也有效。文档位于“基于动态属性的查找器”http://apidock.com/rails /v3.0.0/ActiveRecord/BaseAre you sure your user is saved? I think the right syntax for
find_or_create_by_XX
isBlog.find_or_create_by_title("Some Blog")
. If you need to pass more data you need to usefind_or_initialize
first and set other data after that separately.Loosely related thread: Rails find_or_create by more than one attribute?
--edit
Passing data as hash to
find_or_create_by_XX
seems to work too. Docs are under "Dynamic attribute-based finders" here http://apidock.com/rails/v3.0.0/ActiveRecord/Base试试这个
使用
User.find_or_create
而不是User.find_or_create_by_login
。您的用户对象似乎未保存。
或者在分配 user.id 之前执行 user.reload
try this
use
User.find_or_create
instead ofUser.find_or_create_by_login
.It seems like your user object is not saved.
Or before you assign user.id do user.reload