Rails3, Монгоид, огурец. Не удается установить поле user_id: формат незаконного объекта
-
28-09-2019 - |
Вопрос
У меня есть учетная запись:
class Account
include Mongoid::Document
include Mongoid::Timestamps
referenced_in :user
end
И пользователь:
class User
include Mongoid::Document
include Mongoid::Timestamps
...
references_one :account
...
end
И следующий сценарий (я пытаюсь установить ассоциацию Reail_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:'
Полная версия Backtrace: https://gist.github.com/433ea982d876e1b1fa27.
Я использую: Rails 3.0.3, Ruby 1.9.2, огурец 1.9.4, Машинист 2, Монгоид. Мой Драгоценность
Что я сделал не так?
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')>
Решение
Это может решить ваши проблемы:
Given /^the following accounts:$/ do |class_name, table|
table.hashes.each do |attributes|
User.create(attributes).create_account
end
end
Вы используете пользовательский первичный ключ для User
? Похоже, что мондоид ожидает нормального BSON :: ObjectID, как BSON :: ObjectID ('4Ceaf282b2d3a2ab0000001', но вы проходите простую строку, как 1123322131. В целом вы должны быть осторожны при попытке создать запись и его ассоциации на в то же время