Rails3、Mongoid、黄瓜。无法设置 user_id 字段:ObjectId 格式非法

发布于 2024-10-04 11:28:45 字数 1953 浏览 1 评论 0原文

我有一个帐户模型:

class Account
  include Mongoid::Document
  include Mongoid::Timestamps

  referenced_in   :user
end

和用户:

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  ...

  references_one :account

  ...
end

以及以下场景(我尝试设置reference_one关联):

  Scenario: Client views his account
    Given the following accounts:
      | user_id          |
      |  1123322131      |
         .....

以及以下步骤:

Given /^the following accounts:$/ do |class_name, table|
  table.hashes.each do |attributes|
    Account.create(attributes)
  end
end

当我尝试运行黄瓜时,我总是收到错误:

illegal ObjectId format (BSON::InvalidObjectId)
./features/step_definitions/common_steps.rb:7:in `block (2 levels) in <top (required)>'
./features/step_definitions/common_steps.rb:6:in `each'
./features/step_definitions/common_steps.rb:6:in `/^the following accounts:$/'
features/manage_accounts.feature:8:in `And the following accounts:'

完整版本的回溯:https://gist.github.com/433ea982d876e1b1fa27

我使用:Rails 3.0.3、Ruby 1.9.2、cucumber 1.9.4 ,机械师2,mongoid。我的 Gemfile

我做错了什么?

UPD。没有那么明显的行为:

> a = Account.create :user_id => "123" 
BSON::InvalidObjectId: illegal ObjectId format
> a = Account.create :user_id => 123
=> #<Account _id: 4ceedf055e6f991aef000005, created_at: 2010-11-25 22:11:17 UTC, updated_at: 2010-11-25 22:11:17 UTC, user_id: 123>
> a = Account.create :user_id => "4ceede9b5e6f991aef000007"
=> #<Account _id: 4ceedf1b5e6f991aef000006, created_at: 2010-11-25 22:11:39 UTC, updated_at: 2010-11-25 22:11:39 UTC, user_id: BSON::ObjectId('4ceede9b5e6f991aef000007')>

I have an Account model:

class Account
  include Mongoid::Document
  include Mongoid::Timestamps

  referenced_in   :user
end

and User:

class User
  include Mongoid::Document
  include Mongoid::Timestamps

  ...

  references_one :account

  ...
end

And the following scenario(i try to set reference_one association):

  Scenario: Client views his account
    Given the following accounts:
      | user_id          |
      |  1123322131      |
         .....

And the following step:

Given /^the following accounts:$/ do |class_name, table|
  table.hashes.each do |attributes|
    Account.create(attributes)
  end
end

When I try to run cucumber, I always get an error:

illegal ObjectId format (BSON::InvalidObjectId)
./features/step_definitions/common_steps.rb:7:in `block (2 levels) in <top (required)>'
./features/step_definitions/common_steps.rb:6:in `each'
./features/step_definitions/common_steps.rb:6:in `/^the following accounts:$/'
features/manage_accounts.feature:8:in `And the following accounts:'

full version of backtrace: https://gist.github.com/433ea982d876e1b1fa27

I use: Rails 3.0.3, Ruby 1.9.2, cucumber 1.9.4, machinist 2, mongoid. My Gemfile

What did I do wrong?

UPD. No so obviously behaviour:

> a = Account.create :user_id => "123" 
BSON::InvalidObjectId: illegal ObjectId format
> a = Account.create :user_id => 123
=> #<Account _id: 4ceedf055e6f991aef000005, created_at: 2010-11-25 22:11:17 UTC, updated_at: 2010-11-25 22:11:17 UTC, user_id: 123>
> a = Account.create :user_id => "4ceede9b5e6f991aef000007"
=> #<Account _id: 4ceedf1b5e6f991aef000006, created_at: 2010-11-25 22:11:39 UTC, updated_at: 2010-11-25 22:11:39 UTC, user_id: BSON::ObjectId('4ceede9b5e6f991aef000007')>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

翻身的咸鱼 2024-10-11 11:28:45

这可以解决您的问题:

Given /^the following accounts:$/ do |class_name, table|
  table.hashes.each do |attributes|
    User.create(attributes).create_account
  end
end

您是否为 User 使用自定义主键?看来 Mongoid 期待一个正常的 BSON::ObjectId 像 BSON::ObjectId('4ceeaf282b2d3a2ab0000001' 但你传递的是像 1123322131 这样的纯字符串。一般来说,在尝试创建记录及其关联时必须小心同时

This could solve your problems:

Given /^the following accounts:$/ do |class_name, table|
  table.hashes.each do |attributes|
    User.create(attributes).create_account
  end
end

Are you using a custom primary key for User? It seems that Mongoid is expecting a normal BSON::ObjectId like BSON::ObjectId('4ceeaf282b2d3a2ab0000001' but you are passing a plain string like 1123322131. In general, you have to be careful when trying to create a record and its associations at the same time

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