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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:02:48+00:00 2026-06-13T14:02:48+00:00

I am using Mongoid 2.4.12 and Rails 3.2.8 with cocoon . My nested form

  • 0

I am using Mongoid 2.4.12 and Rails 3.2.8 with cocoon. My nested form appears to work flawlessly on the front end, but the nested model and relation are not being saved.

Project

  include Mongoid::Document
  include Mongoid::Paranoia
  include Mongoid::Timestamps

  belongs_to :user
  has_many :tasks

  accepts_nested_attributes_for :tasks, allow_destroy: true
  attr_accessible :name, :completed, :tasks_attributes

  field :name
  field :completed, type: Boolean

  validates_presence_of :name

Task

  include Mongoid::Document
  include Mongoid::Paranoia
  include Mongoid::Timestamps

  belongs_to :user
  belongs_to :category
  belongs_to :projects

  accepts_nested_attributes_for :category, allow_destroy: false
  attr_accessible :title, :description

  field :title
  field :description

Project View

<%= form_for(@project) do |f| %>
    <% if @project.errors.any? %>
        <div id="error_explanation">
            <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
            <ul>
                <% @project.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                <% end %>
            </ul>
        </div>
    <% end %>
    <div class="row-fluid field-set">
        <div class="field">
            <%= f.text_field :name, placeholder: 'Name' %>
        </div>
        <div class="buttonset">
            <%= f.radio_button :completed, true, checked: true, class: 'completed-button inline' %>
            <%= label :completed, 'Completed', value: true, class: 'inline' %>
        </div>
    </div>
    <div class="row-fluid tasks-container">
        <%= link_to_add_association 'Add task', f, :tasks, class: 'btn', id: 'task-button' %>
        <%= f.fields_for :tasks do |task| %>
            <%= render 'task_fields', f: task %>
        <% end %>
    </div>
    <%= f.submit 'Save', class: 'btn', id: 'save-button' %>
<% end %>

Task Partial

<div class="new_task">
    <div class="nested-fields task-container">
        <%= link_to_remove_association 'x', f, class: 'close' %>
        <div class="field-set">
            <%= f.collection_select(:category, Category.all, :id, :name, { prompt: 'Task Type' }, { class: 'category-selector' } ) %>
        </div>
        <div class="field-set">
            <%= f.text_field :name, placeholder: 'Name', class: 'name-field' %>
        </div>
        <div class="field-set">
            <%= f.text_field :description, placeholder: 'Description', class: 'description-field' %>
        </div>
    </div>
</div>

Here are my params:

Started POST "/projects" for 127.0.0.1 at 2012-10-29 01:36:01 -0500
Processing by ProjectsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"rNX8DvKuEJgjxBvJ5c5ArTjtIMR5Uy6DtCCou0wHswk=", "project"=>{"name"=>"Example Name", "completed"=>"true", "tasks_attributes"=>{"1351491343724"=>{"_destroy"=>"", "category"=>"5089e01023499d9904000004", "title"=>"Example Title", "description"=>"Example description."}}}, "commit"=>"Save"}
MONGODB myapp_development['users'].find({:deleted_at=>nil, :_id=>BSON::ObjectId('5089a25523499d92d8000004')}).sort([[:_id, :asc]])
MONGODB myapp_development['categories'].find({:deleted_at=>nil, :_id=>BSON::ObjectId('5089e01023499d9904000004')}).sort([[:_id, :asc]])
MONGODB myapp_development['projects'].insert([{"_id"=>BSON::ObjectId('508e23d123499d2a6c00000b'), "name"=>"Example Name", "completed"=>true, "user_id"=>BSON::ObjectId('5089a25523499d92d8000004'), "updated_at"=>2012-10-29 06:36:01 UTC, "created_at"=>2012-10-29 06:36:01 UTC}])
Redirected to http://localhost:3000/projects/508e23d123499d2a6c00000b
Completed 302 Found in 24ms

As you can see, I am not getting any errors regarding accepts_nested_attributes_for, etc.

In my projects#create action, I can debug after @project.save and see that @project.tasks correctly returns an array of one task. However this task is never created/inserted/saved, and there is no relation afterward. Any suggections on what I might be missing?

  • 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-13T14:02:49+00:00Added an answer on June 13, 2026 at 2:02 pm

    According to the Mongoid relation docs, child elements are not automatically persisted. I am unclear on why mine are not, since accepts_nested_attributes_for should resolve this. Regardless, setting autosave: true on the relation will resolve my original problem. And for the record, cocoon will also work with has_and_belongs_to_many. Brilliant!

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

Sidebar

Related Questions

I am using Mongoid on Rails 3 and trying to build a nested form.
Using Mongoid 2.4.5 on Rails 3.2.1 I have a Model Book that has_many :pages
I would like to write a query in a Rails model using mongoid, and
Newbie question here. I am using mongoid, rails 3.2 and nested_form for my nested
I'm using Mongoid to work with MongoDB in Rails. What I'm looking for is
I am using mongoid in rails app. rails 3.0.10 ruby 1.9.2p0 When I am
I have a class User in Rails using Mongoid and Devise. I can't seem
I am currently working on a rails app where we are using mongoid/mongoDB on
I've got a Ruby on Rails app that is using MongoDB for datastorage. Mongoid::Timestamp
I am talking to multiple databases using Mongoid.override_database(database_name) using Mongoid with rails. How do

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.