ruby on rails - Mongoid: embedded one to one, with pure json input -
i'm trying embedded 1 one mongoid model "embedded child" pure json input coming external api.
the parent document defined follow:
./app/models/user.rb
class user include mongoid::document field :nickname, :type => string embeds_one :watchlist def self.create_with_omniauth(auth) create! |user| user.nickname = auth['user_info']['nickname'] end end end the child defined follow (using mix of "mongo ruby driver" & "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 the controllers this:
app/controllers/home_controller.rb
class homecontroller < applicationcontroller def index if current_user nickname = request.env["omniauth.auth"] @watched = watchlist.watched(nickname) end end end app/controllers/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 now, gitwatch_dev mongodb, 2 unrelated "mongo models", in mongo cli follow:
> db.users.find() { "_id" : objectid("4e117b951d41c80b14000001"), "provider" : "github", "uid" : "1573", "name" : "luca g. soave", "email" : "luca.soave@gmail.com", "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\"=>\"luca.soave@gmail.com\"}" } > 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 object oriented read/write access git repositories via ruby.", "name" : "grit", "svn_url" : "https://svn.github.com/mojombo/grit", "ssh_url" : "git@github.com:mojombo/grit.git", "clone_url" : "https://github.com/mojombo/grit.git", "forks" : 140 } ... ... while i'd nested "child parend" json, embedded 1 one mongoid model example:
{ "_id" : objectid("4d3ed089fb60ab534684b7e9"), "name" : { "_id" : objectid("4d3ed089fb60ab534684b7e0"), "vorname" : "heinrich", "nachname" : "heine" } } than, i'm looking doing in "pure mongoid" without mess of ruby drivers, cannot find out way ...
update jul 6 2011 - rubish gupta:
it works
user.create_watchlist['dynamic_attribute'] = resp.body in user parent model: app/models/user.rb see full code @ http://www.pastie.org/2169671
the problem here these lines:
db = mongo::connection.new.db('gitwatch_dev') coll = db.collection('watchlist') coll.insert(resp.body) here doing accessing collection watchlist, not want create , want embed records in users collection.
correct way make function watch(repo) following in user:
def watch(repo) # obtain resp self.create_watchlist(resp.body) end but should enable dynamic attributes in mongoid.yml this.
in controller should current_user.watch(repo) instead of 3 lines noted above.
Comments
Post a Comment