2013-02-06 77 views
1

具有以下工廠:RSpec的/ FactoryGirl - MongoID嵌入文檔不會保留

FactoryGirl.define do 
    factory :user do 
    provider "github" 
    sequence(:uid) {|n| "111111#{n}"} 
    sequence(:name) {|n| "name#{n}"} 
    sequence(:email) {|n| "name#{n}@example.com"} 
    sequence(:nickname) {|n| "nick#{n}"} 
    sequence(:token) {|n| "111111111111111111111111111111111111111#{n}"} 
    watchlists { [ FactoryGirl.build(:watchlist) ] } 
    end 
    factory :watchlist do 
     sequence(:html_url) {|n| "https://github.com/user#{n}/repo#{n}"} 
     description "One repo description" 
     sequence(:forks) {|n| "1#{n}"} 
     sequence(:watchers) {|n| "#{n}"} 
     created_at "2012-11-21 00:09:12 UTC" 
     pushed_at "2010-04-15 21:11:51 UTC" 
     avatar_url "https://avatar.example.com/avatar.png" 
    end 
end 

當我創建的工廠類似如下:

let(:user) { FactoryGirl.create :user } 

它導致:用戶只堅持模型,但沒有其嵌入的孩子:關注列表:

1.9.3p194 :014 > User.all.entries 
    MOPED: 10.8.0.6:27017 COMMAND  database=admin command={:ismaster=>1} (328.6924ms) 
    MOPED: 192.168.3.101:27017 COMMAND  database=admin command={:ismaster=>1} (216.3296ms) 
    MOPED: 127.0.0.1:27017 COMMAND  database=admin command={:ismaster=>1} (422.3838ms) 
    MOPED: 127.0.0.1:27017 QUERY  database=mongoid_test collection=users selector={} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (354.1191ms) 
=> [#<User _id: 51126ae3aaa0786654000001, _type: nil, created_at: 2013-02-06 14:38:27 UTC, updated_at: 2013-02-06 14:38:27 UTC, email: "[email protected]", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, provider: "github", uid: "1111111", name: "name1", nickname: "nick1", token: "1111111111111111111111111111111111111111", secret: nil, user_hash: nil>] 
1.9.3p194 :015 > 

如何獲得嵌入式mongoid模型還堅持?

+0

會發生什麼當你嘗試'User.all.each {| user | p user.watchlists}' –

回答

1

它應該是持久的,但你需要查詢關係。

嘗試:

User.first.watchlists

還是在MongoDB的外殼,整個文件將打印出像你期望 db.users.find().pretty();

更多關於MongoDB的信息外殼這裏:http://docs.mongodb.org/manual/mongo/

+0

是的,謝謝你,我只是缺少一些查詢:-),而嵌入式模型已經存在了...... –