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 6835885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:17:12+00:00 2026-05-26T23:17:12+00:00

I have following User model, embeds the Category model, class User include Mongoid::Document include

  • 0

I have following User model, embeds the Category model,

class User
  include Mongoid::Document
  include BCrypt

  field :email,           :type => String
  field :password_hash,   :type => String
  field :password_salt,   :type => String

  embeds_many :categories
  embeds_many :transactions
  ....
   end

My question is, I just found that if I use the code:

me = User.where("some conditions")
me.categories << Category.new(:name => "party")

everything works fine, but if I use the .create method:

me = User.where("some conditions")
me.categories << Category.create(:name => "party")

I will get an exception:

undefined method `new?' for nil:NilClass

Anyone knows why is that? And from mongoid.org http://mongoid.org/docs/persistence/standard.html, I could see that .new and .create actually generates the same mongo command.

Needs help, thanks 🙂

  • 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-26T23:17:12+00:00Added an answer on May 26, 2026 at 11:17 pm

    Create immediately persist the document into mongo. Since the category document is within another document (as embedded) you cannot save it separately. Thats why you are getting the error.

    For more clarity, assume embedded document as a field in the parent
    document which contains sub fields. Now you can easily understand that
    you cannot save a field without a document. right?

    Other hand new  initialize the document class and will be only inserted into the parent doc when using <<.

    Category.create(:name => "party")
    >>NoMethodError: undefined method `new?' for nil:NilClass
    

    is equivalent to

     c = Category.new(:name => "party")
     c.save
     >>NoMethodError: undefined method `new?' for nil:NilClass
    

    Hope this helps

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

Sidebar

Related Questions

Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
I have the following model: class User: name = models.CharField( max_length = 200 )
In my GWT app I have the following model class: import com.google.gwt.user.client.rpc.IsSerializable; public class
I have the following model structure class User(db.Model) : nickname = db.StringProperty(required=True) fullname =
I have the following models: class Application(models.Model): users = models.ManyToManyField(User, through='Permission') folder = models.ForeignKey(Folder)
I have following model: class UserProfile(models.Model): User profile model, cintains a Foreign Key, which
I have the following models -- class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) ... class
I have the following models: class UserProfile(models.Model): user = models.OneToOneField(User) score = models.PositiveIntegerField() class
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user
I have the following schema User: columns: id: type: integer primary: true name: string

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.