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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:35:44+00:00 2026-05-20T10:35:44+00:00

I’ve got a Rails 3.0 project, using mongo with MongoMapper. I’ve got a model

  • 0

I’ve got a Rails 3.0 project, using mongo with MongoMapper. I’ve got a model with basic info describing a petstore which has_many pets. A pet is a separate model.

I have a form that lets me create new petstores, but how do I add a field to create a pet at the same time as I create the new store? Right now I have a hacked in solution that accomplishes what I want, but I there’s probably a Rails Way to do this, huh? How can I do it properly, so that I can use validations on the form fields and such?

My current solution involves hacking in a form field for the pet manually (added an tag with name=”petstore[pet]” in the form template. This form is handled by petstore_controller’s create method, and I added code to create a pet from the form field

Models:

class Petstore
  include MongoMapper::Document

  many :pets, :dependent => :destroy

  key :name, String
  key :address, String
end

class Pet
  include MongoMapper::Document

  belongs_to :petstore

  key :petstore_id, ObjectID, :required=>true
  key :type, String, :required=>true
  key :name, String 
end

_form.html.erb

<%=form_for @petstore do |f| %>
  <li>
    <%= f.label :name %>
    <%= f.text_field :name, :placeholder =>"The name" %>
  </li>
  <li>
    <%= f.label :address %>
    <%= f.text_field :address, :placeholder =>'The address' %>
  </li>
  <li>
    <label for="petstore_pet">Type of pet</label>
    <input type="text" id="petstore_pet" name="petstore[pet]"> 
  <li>
    <%= f.submit "Submit" %>
  </li> 
<% end %>

petstores_controller.rb

def create
  pet = @petstore.pets.build :type => params[:petstore][:pet]
  pet.save if pet

  respond_to do |format| 
    ...
  end
end

Similar topics/questions:

  • Rails + MongoMapper + EmbeddedDocument form help

(I’m not exactly sure how to map that solution onto my question.)

  • Does MongoMapper (or any other Mongodb adapter) have a method like "accepts_nested_attributes_for"?

(Accepted answer references a google group thread that is a little over my head…)

  • 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-20T10:35:45+00:00Added an answer on May 20, 2026 at 10:35 am

    Short answer, there is not a clean, simple way to do this.

    Slightly longer answer:

    Your controller will attempt to create/update a model by passing it the hash derived from the form. The key to successfully nesting forms is first to get your form to give you back the correct data. I think you have gotten this part down. Second, you need to get PetStore.create to do “the right thing” with that hash.

    In MongoMapper the rules for dealing with the hash are very simple. It creates and instance of your model and then calls #{key}= value for every key/value pair in the hash. For example if MyModel is a MongoMapper Document class and I call MyModel.create(:foo=>"bar",:baz=>5) MongoMapper creates in instance of MyModel and calls foo="bar" and baz=5 on it. So long as those methods exist, MongoMapper does not care what they do. (Note the key class method creates these methods for each key.)

    To get this all to work you need to create a pet= method on PetStore and have it build out a pet model.

    This is not by a perfect answer, but it should be enough for you to get something to work.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.