Rails3, mongóide, pepino. Não é possível definir o usuário user_id: formato de objeto ilegal

StackOverflow https://stackoverflow.com/questions/4272885

  •  28-09-2019
  •  | 
  •  

Pergunta

Eu tenho um modelo de conta:

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

  referenced_in   :user
end

e usuário:

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

  ...

  references_one :account

  ...
end

E o seguinte cenário (tento definir a Reference_One Association):

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

E a seguinte etapa:

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

Quando tento executar o pepino, sempre recebo um erro:

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:'

Versão completa do backtrace: https://gist.github.com/433ea982d876e1b1fa27

Eu uso: Rails 3.0.3, rubi 1.9.2, pepino 1.9.4, maquinista 2, mongóide. Meu GemFile

O que eu fiz errado?

Up. Não, então, obviamente, comportamento:

> 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')>
Foi útil?

Solução

Isso pode resolver seus problemas:

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

Você está usando uma chave primária personalizada para User? Parece que o Mongoid está esperando um BSON :: ObjectId normal como BSON :: ObjectId ('4ceAd282B2D3A2AB0000001', mas você está passando uma corda simples como 1123322131. Em geral, você deve ter cuidado ao tentar criar um registro e suas associações no mesmo tempo

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top