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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:00:40+00:00 2026-06-04T02:00:40+00:00

I need to have a model which will behave like a embedded and not-embedded.

  • 0

I need to have a model which will behave like a embedded and not-embedded.

For example if I want to store this model as embedded:

class MenuPosition
  include Mongoid::Document
  field :name, type: String
  field :category, type: String

I need to add

  embedded_in :menu

to it.

On the other side, if I add this line in the model I cannot store this model as not-embedded:

position = {
            "name" => "pork",
            "category" => "meal",
            "portion" => 100
        }
MenuPosition.create(position)

error message:

NoMethodError:
       undefined method `new?' for nil:NilClass

Can I use one model for embedded and not-embedded documents?

  • 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-06-04T02:00:41+00:00Added an answer on June 4, 2026 at 2:00 am

    In our project we had a similar thing. What we did is define the fields as a module. A bit like this:

    module SpecialFields
      extend ActiveSupport::Concern
    
      included do
        field :my_field, type: String
        field :my_other_field, type: String
      end
    end
    

    Then in your class where you want to embed, just do:

    include SpecialFields
    

    In your class that you’d like to store separately as a non-embedded document, do this:

    class NotEmbeddedDoc
      include Mongoid::Document
    
      include SpecialFields
    end
    

    This worked pretty well in our project for a few things. However, it might not be appropriate in your case since you want to embed many. This only really works for embeds one cases I think. I have posted it here in case it helps people.

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

Sidebar

Related Questions

Need help figuring this out. Have a product model: class Product :name :short_description :price
I have a need for a model(?) on my app which basically contains a
I have a form which will include some optional questions that need to asked
I'm making an MMO which will have a 2d world. (This is a learning
I have a model which has many of another model but this model only
I have a model - Category which HABTM Blog. I need to create a
1] I have the following model: class UserReportedData(db.Model): #country selected by the user, this
I have a django model which I would like to edit/add via the admin
I have a prototype model where I need to include the following extension methods
I have the following model association: a student model and has_many scores. I need

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.