Rails3, Mongoid, Gurke. Das Feld user_id kann nicht festgelegt werden: Illegales ObjectID -Format

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

  •  28-09-2019
  •  | 
  •  

Frage

Ich habe ein Kontomodell:

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

  referenced_in   :user
end

und Benutzer:

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

  ...

  references_one :account

  ...
end

Und das folgende Szenario (ich versuche Reference_one Association festzulegen):

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

Und den folgenden Schritt:

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

Wenn ich versuche, Gurken auszuführen, bekomme ich immer einen Fehler:

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

Vollversion von Backtrace: https://gist.github.com/433ea982d876e1b1fa27

Ich verwende: Rails 3.0.3, Ruby 1.9.2, Gurke 1.9.4, Maschinist 2, Mongoid. Mein GemFile

Was habe ich falsch gemacht?

Aktualisierung. Nein, so offensichtlich Verhalten:

> 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')>
War es hilfreich?

Lösung

Dies könnte Ihre Probleme lösen:

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

Verwenden Sie einen benutzerdefinierten Primärschlüssel für User? Es scheint, dass Mongoid einen normalen bson :: objectID wie bson :: objectID ('4eeAEF282B2D3A2AB0000001' 'erwartet, aber Sie übergeben eine einfache Zeichenfolge wie 1123322131. Im Allgemeinen müssen Sie vorsichtig sein, wenn Sie versuchen, eine Aufzeichnung und ihre Assoziationen in der gleiche Zeit

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top