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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:06:29+00:00 2026-06-12T08:06:29+00:00

I have the following models: class User include Mongoid::Document embeds_many :user_missions attr_accessible :user_missions_attributes accepts_nested_attributes_for

  • 0

I have the following models:

class User
  include Mongoid::Document
  embeds_many :user_missions
  attr_accessible :user_missions_attributes
  accepts_nested_attributes_for :user_missions, :allow_destroy => true
end

class UserMission
  include Mongoid::Document

  embedded_in :user, :inverse_of => :user_missions

  belongs_to :mission, :inverse_of => nil
  validates_presence_of :mission, :inverse_of => nil

  attr_accessible :mission_title

  def mission_title
    mission.try(:title)
  end

  def mission_title=(title)
    self.mission = Mission.find_or_create_by(:title => title) if title.present?
  end
end

class Mission
  include Mongoid::Document

  attr_accessible :title
  field :title, type: String
  validates_presence_of :title
end

The problem is I am having trouble deleting a user_mission from a user.

I failed in my view (basically verbatim from a railscasts):

jQuery ->
  $('form').on 'click', '.remove_fields', (event) ->
    $(this).prev('input[type=hidden]').val('1')
    $(this).closest('fieldset').hide()
    event.preventDefault()

<fieldset>
  <%= f.object.mission.title %>
  <%= f.hidden_field :_destroy %>
  <%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>

and I seem to have a problem in the following spec

it "should delete using nested attributes" do
  user = User.create(:username => "username", :email => "user@example.com", :password => "password", :password_confirmation => "password")
  user.attributes = { 
    user_missions_attributes: { 
      "0" => { mission_title: "Mission A" } } }
  user.save!
  saved_user = User.first
  saved_user.user_missions.size.should == 1

  saved_user.attributes = { 
    user_missions_attributes: { 
      "0" => { :_destroy => '1' } } }
  saved_user.save!
  emptiedUser = User.first
  debugger

  emptiedUser.user_missions.size.should == 0
end

It fails on the last line it finds 1 UserMission.

I am using Mongoid 3.0.6 and Rails 3.2.8. Any help would be appreciated.

  • 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-12T08:06:30+00:00Added an answer on June 12, 2026 at 8:06 am

    You need to include id or _id when destroying. The “0” there is just a key in user_missions_attributes. Inside the hash that “0” keys to you need the id of the document you actually want to delete. So something like:

    "0" => { :_id => ..., :_destroy => '1' }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following User model, embeds the Category model, class User include Mongoid::Document include
I have the following models class Track include Mongoid::Document field :artist, type: String field
I have the following subclass: class UserProfile(User): user = models.OneToOneField(User, parent_link=True) And an UserProfileAdmin:
I have the following abilities model: class Ability include CanCan::Ability def initialize(user) user ||=
Suppose I have the following models: class User(models.Model): pass class A(models.Model): user = models.ForeignKey(User)
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
I have the following models: class ProjectUser(models.Model): categories = models.ManyToManyField('UserCategory', blank=True, null=True) user_id =
I am using Rails 3.2. I have following models: Blog Comment User class Blog
I have the following model: class User: name = models.CharField( max_length = 200 )
I currently have the following models: class Player < ActiveRecord::Base belongs_to :team belongs_to :user

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.