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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:39:33+00:00 2026-06-12T02:39:33+00:00

I want to be able to replace the entire set of embedded documents in

  • 0

I want to be able to replace the entire set of embedded documents in a MongoDB object on save – the HTML form will contain the entire new set.

I also want it to validate everything before saving – i.e. don’t trash the old documents, then validate each one as it gets added.

I’ve come up with an implementation, but it’s not persisting – none of the new embedded documents show up. An added complication is that there is inheritance involved. Here’s (simplified) the set of models I have so far:

class Person
  include Mongoid::Document
  embeds_many :vehicles
end

class Vehicle 
  include Mongoid::Document
  embedded_in :person
end

class Car < Vehicle
end

class Motorbike < Vehicle
end

In order to work out what kind of vehicle to instantiate when the user submits the form, I’ve added this method to the Person class:

def build_from_hash(hash)
   @vehicles= []
   hash.each do |idx, vehicle|
      if vehicle[:_type].constantize < Inclusion # Check for inheritance, for security
         self.vehicles.push vehicle[:_type].constantize.new(vehicle)
      end
   end
end

And modified the controller to call it:

def submit_build
  @person= current_user.persons.find(params[:id])
  @person.build_from_hash(params[:vehicles]) if params.has_key? :vehicles

  respond_to do |format|
    if @person.save # Also tried: @person.update_attributes(inclusions: @person.vehicles)
      format.html { redirect_to @person, notice: 'Person was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: "build" }
      format.json { render json: @person.errors, status: :unprocessable_entity }
    end
  end
end

No errors generated – the page redirects as if it had worked, but when I examine it again there are no embdedded documents.

Using Rails 3.2.8, Mongoid 3.0.5, MongoDB 1.8.3.

  • 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-12T02:39:35+00:00Added an answer on June 12, 2026 at 2:39 am

    I figured out how to do this in a more standard Rails-esque way, albeit with a workaround for some peculiar behaviour.

    Short answer: the Person class needed to accepts_nested_attributes_for :vehicles and I had to remove all the custom stuff from the controller, replacing the action with a standard update action. But it wouldn’t accept the _type parameter for the subclasses of Vehicle unless the Rails process had already instantiated an object of each of those subclasses, so I was forced to work around it with an initialiser like this:

    Car.new
    Motorbike.new
    

    Long answer is on the Mongoid Google group.

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

Sidebar

Related Questions

I want to be able to replace style=&quot;STUFF&quot; I keep thinking that this is
I want to be able to do the following actions with a form submit
I want to be able to replace the numbers between the last two octets
I want to be able to replace spaces with - but also want to
Hey guys I want to be able to replace a tuple in a list
I want to replace \cite{foo123a} with [1] and backwards. So far I was able
I want to be able to replace specific pattern in .java files on build
I want to be able to replace a string in a file using regular
I want to be able to replace a line of text in a file
I want to be able to replace Accounts tab on my navigation bar with

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.