Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6107153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:09:28+00:00 2026-05-23T14:09:28+00:00

I’m trying embedded one to one mongoid model where the embedded child is a

  • 0

I’m trying embedded one to one mongoid model where the “embedded child” is a pure json input coming from an external API.

The parent document is defined like follow:

./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

the child is defined like follow (using a 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 look like 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, what I get is a gitwatch_dev mongodb, with two UNRELATED “mongo models”, in mongo cli they look like the 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 you object oriented read/write access to 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 like to get a nested “child into parend” json, something like the embedded one to one mongoid model example:

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

Than, I’m also looking for doing that in “pure mongoid” without mess of ruby drivers, but cannot find out the way …

UPDATE jul 6 2011 – thanks to Rubish Gupta:

it finally works with

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

in the User parent model: app/models/user.rb see the full code at http://www.pastie.org/2169671

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:09:28+00:00Added an answer on May 23, 2026 at 2:09 pm

    The problem here is with these lines:

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

    Here what you are doing is accessing the collection watchlist, which you do not want to create and want to embed the records in users collection.

    Correct way to do would be to make a function watch(repo) like following in User:

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

    But you should enable dynamic attributes in mongoid.yml to do this.

    In you controller you should do current_user.watch(repo) instead of the three lines noted above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.