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

  • Home
  • SEARCH
  • 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 7562229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:21:51+00:00 2026-05-30T13:21:51+00:00

I’m storing social media postings from a REST API to Mongoid. I’m using the

  • 0

I’m storing social media postings from a REST API to Mongoid.

I’m using the very basic User/Post model:

   class Post
     include Mongoid::Document
     belongs_to :user
   end # post

   class User
     include Mongoid::Document
     has_many :posts
   end # post

Now let say the parsed JSON object retrieved from the API is:

hash = {
  "id" : "7890",
  "text": "I ate foo bar tonight",
  "user": {
     "id" : "123",
     "name" : "beavis"
   }
}
   p = Post.new(hash)
   p.save

This will save the object as:

   {
      "id" : "7890",
      "text": "I ate foo bar tonight",
      "user_id": "123"
    }

Now how do I go about saving the user object too? p.user.save will work, but I want to know…

  1. I’ll have to check for the user object is already in mongodb or not. I’m using User.find(p.user.id). But does User.find() only look for the ID? Or does find() also load the whole user object?
  2. I’m overriding the Post.create() method to do this right now. Is that bad?
  3. What is the best place to save child objects? Do I check for user existence in Post.create()? Post.before_create()? Post.after_create()? Or something else?
  4. Is there a difference with p.user.save and User.create(p.user)??
  • 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-30T13:21:52+00:00Added an answer on May 30, 2026 at 1:21 pm

    Can’t you use accepts_nested_attributes_for?

    class Post
      include Mongoid::Document
      belongs_to :user
      accept_nested_attributes_for :user
    end # post
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build

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.