2010-11-25 1 views
0

Rails3, Mongoid, 오이. user_id 필드를 설정할 수 없습니다 : 잘못된 ObjectId 형식

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 
Given /^the following accounts:$/ do |class_name, table| 
    table.hashes.each do |attributes| 
    Account.create(attributes) 
    end 
end 
xucx를 실행하려고하면 항상 오류가 발생합니다.

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

I use : Rails 3.0.3, Ruby 1.9.2, 오이 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')> 

답변

2

이 문제를 해결할 수 :

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

당신이 User에 대한 사용자 정의 기본 키를 사용하고 있습니까? Mongoid는 BSON :: ObjectId ('4ceeaf282b2d3a2ab0000001')와 같은 일반 BSON :: ObjectId를 기대하지만, 1123322131과 같은 평범한 문자열을 전달하는 것으로 보입니다. 일반적으로 레코드 및 해당 연관을 만들 때주의해야합니다. 같은 시간에

+0

UPD를 참조하십시오 해결책은 "4ceede9b5e6f991aef000007"와 같은 ID를 사용합니다 – petRUShka

+0

사용자를 식별하고 Mongoid에게 알려주는'username' 또는'email'와 같은 오이 사양에 다른 필드를 사용 하시길 권합니다. – bowsersenior

+0

나는이 방법을 이해하려고 노력했다. 당신이 나를 도우면 위대 할 것이다. http://stackoverflow.com/questions/4281386/rails- 오이 만든 개체와 협회 – petRUShka

관련 문제