2011-07-04 21 views
0

我在嘗試embedded one to one mongoid model其中「嵌入的孩子」是來自外部API的純json輸入。Mongoid:一對一嵌入,純json輸入

父文件的定義如下如下:

./app/models/user.rb

class User 
    include Mongoid::Document 

    field :nickname, :type => String 

    embeds_one :watchlist 

    def self.create_with_omniauth(auth) 
    create! do |user| 
     user.nickname = auth['user_info']['nickname'] 
    end 
    end 
end 

孩子是這樣定義如下(使用 「蒙戈Ruby驅動程序」 的混合&「 mongoid ORM「):

./app/models/watchlist.rb

require 'mongo' 
class Watchlist 
    include Mongoid::Document 

    embedded_in :user 

    def self.watched(nickname) 
    conn = FaradayStack.build 'https://api.github.com' 
    #resp = conn.get '/users/:nickname/watched' 
    resp = conn.get '/users/lgs/watched' 

    db = Mongo::Connection.new.db('gitwatch_dev') 
    coll = db.collection('watchlist') 
    coll.insert(resp.body) 
    end 
end 

個的控制器是這樣的:

應用程序/控制器/ home_controller.rb

class HomeController < ApplicationController 
    def index 
    if current_user 
     nickname = request.env["omniauth.auth"] 
     @watched = Watchlist.watched(nickname) 
    end 
    end 
end 

應用程序/控制器/ sessions_controller.rb

class SessionsController < ApplicationController 
    def create 
    auth = request.env["omniauth.auth"] 
    #user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth) 
    user = User.where(:provider => auth['provider'], :uid => auth['uid']).first || User.create_with_omniauth(auth) 
    session[:user_id] = user.id 
    redirect_to root_url, :notice => "Signed in!" 
    end 
... 
end 

現在,我得到的是一個gitwatch_dev MongoDB中,與兩個不相關的「mongo模型」,在mongo cli中,它們看起來像下面這樣:

> db.users.find() 
{ "_id" : ObjectId("4e117b951d41c80b14000001"), "provider" : "github", "uid" : "1573", "name" : "Luca G. Soave", "email" : "[email protected]", "nickname" : "lgs", "token" : "a512434559b07feb0a98d199238764sde9876", "secret" : null, "user_hash" : "{\"plan\"=>{\"name\"=>\"free\", \"collaborators\"=>0, \"space\"=>307200, \"private_repos\"=>0}, \"gravatar_id\"=>\"9c7d80ebc20ab8xx994e57519ae\", \"company\"=>\"http://www.linkedin.com/in/lucasoave\", \"name\"=>\"Luca G. Soave\", \"created_at\"=>\"2008/02/28 05:26:40 -0800\", \"location\"=>\"Milan - Italy\", \"disk_usage\"=>113860, \"collaborators\"=>0, \"public_repo_count\"=>32, \"public_gist_count\"=>85, \"blog\"=>nil, \"following_count\"=>140, \"id\"=>1573, \"owned_private_repo_count\"=>0, \"private_gist_count\"=>2, \"type\"=>\"User\", \"permission\"=>nil, \"total_private_repo_count\"=>0, \"followers_count\"=>9, \"login\"=>\"lgs\", \"email\"=>\"[email protected]\"}" } 

> db.watchlist.find() 
{ "_id" : ObjectId("4e117bd31d41c80b14000002"), "open_issues" : 47, "url" : "https://api.github.com/repos/mojombo/grit", "watchers" : 997, "homepage" : "http://grit.rubyforge.org/", "master_branch" : null, "language" : "Ruby", "fork" : false, "pushed_at" : "Sat Jul 02 2011 01:02:45 GMT+0200 (CEST)", "created_at" : "Mon Oct 29 2007 15:37:16 GMT+0100 (CET)", "git_url" : "git://github.com/mojombo/grit.git", "html_url" : "https://github.com/mojombo/grit", "private" : false, "size" : 2482, "owner" : { "url" : "https://api.github.com/users/mojombo", "login" : "mojombo", "avatar_url" : "https://secure.gravatar.com/avatar/25c7c18223fb42a4c6ae1c8db6f50f9b?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png", "id" : 1 }, "description" : "Grit gives you object oriented read/write access to Git repositories via Ruby.", "name" : "grit", "svn_url" : "https://svn.github.com/mojombo/grit", "ssh_url" : "[email protected]:mojombo/grit.git", "clone_url" : "https://github.com/mojombo/grit.git", "forks" : 140 } 
... 
... 

w往往微不足道我想獲得一個嵌套的「孩子變成parend」 JSON,有點像embedded one to one mongoid model例如:

{ 
    "_id" : ObjectId("4d3ed089fb60ab534684b7e9"), 
    "name" : { 
    "_id" : ObjectId("4d3ed089fb60ab534684b7e0"), 
    "vorname" : "Heinrich", 
    "nachname" : "Heine" 
    } 
} 

比,我也期待這樣做,在「純mongoid」沒有紅寶石司機的一塌糊塗,卻找不到出路......

UPDATE 2011年7月6日 - 多虧Rubish古普塔:

它最終與

user.create_watchlist['dynamic_attribute'] = resp.body 

工作在用戶父模型:應用/models/user.rb看到完整的代碼在http://www.pastie.org/2169671

回答

1

這裏的問題是,這些行:

db = Mongo::Connection.new.db('gitwatch_dev') 
coll = db.collection('watchlist') 
coll.insert(resp.body) 

這裏你正在做的是訪問該集合監視列表,您不希望創建和想要在用戶集合中嵌入記錄。

正確的方式做是使一個函數watch(repo)User如下:

def watch(repo) 
    # obtain the resp 
    self.create_watchlist(resp.body) 
end 

但是,你應該能夠動態屬性在mongoid.yml做到這一點。

在你的控制器,你應該做current_user.watch(repo),而不是上面提到的三條線。

+0

非常感謝Rubish Gupta。 –