Rails 种子混乱

发布于 2024-10-09 13:17:02 字数 839 浏览 2 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

樱娆 2024-10-16 13:17:02

我明白问题出在哪里了。未填充的字段未在其各自模型的 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.

中二柚 2024-10-16 13:17:02

该代码很好,并且是 find_or_create 的正确语法。正如其他人所说,最有可能的问题是用户无效。如果用户无效,尝试调用 user.reload 会使其崩溃,因此会使问题更加明显,但从中得到的错误将是无用的(它会抱怨无法找到没有 id 的用户)。

不幸的是,如果 find_or_create 无效,则它不能作为引发异常的 bang 方法,因此最好的办法可能是引发错误并在尝试创建用户后输出错误:

user = User.find_or_create_by_login(:login => "myname")
raise "User is invalid: #{user.errors.full_messages}" unless user.valid?

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 call user.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 = User.find_or_create_by_login(:login => "myname")
raise "User is invalid: #{user.errors.full_messages}" unless user.valid?
那支青花 2024-10-16 13:17:02

用户创建成功?如果是这样..尝试 user.reload 如果没有。这可能是错误

User created with success? if so..try user.reload if not. that is probably the error

老旧海报 2024-10-16 13:17:02

您确定您的用户已保存吗?我认为 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/Base

Are you sure your user is saved? I think the right syntax for find_or_create_by_XX is Blog.find_or_create_by_title("Some Blog"). If you need to pass more data you need to use find_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

鸠魁 2024-10-16 13:17:02

试试这个
使用 User.find_or_create 而不是 User.find_or_create_by_login
您的用户对象似乎未保存。
或者在分配 user.id 之前执行 user.reload

user = User.find_or_create(  
  :login => "myname",  
  :email => "[email protected]",  
  :user_type => "Admin",  
  :password => "admin",  
  :password_confirmation => "admin")

[user.reload]

project = Project.find_or_create_by_user_id(  :user_id => user.id,
  :name => "Test Project")

try this
use User.find_or_create instead of User.find_or_create_by_login.
It seems like your user object is not saved.
Or before you assign user.id do user.reload

user = User.find_or_create(  
  :login => "myname",  
  :email => "[email protected]",  
  :user_type => "Admin",  
  :password => "admin",  
  :password_confirmation => "admin")

[user.reload]

project = Project.find_or_create_by_user_id(  :user_id => user.id,
  :name => "Test Project")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文