Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-629

Nested attributes not persisting

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.1.8
    • Affects Version/s: None
    • Component/s: None

      Hi

      This code used to work in beta20. I doesn't work as of RC6.
      class CourseGroup
      include Mongoid::Document

      field :name
      referenced_in :school
      references_and_referenced_in_many :courses
      accepts_nested_attributes_for :courses, :reject_if => lambda

      { |a| a[:name].blank? }

      , :allow_destroy => true

      end

      #In the Controller:
      def create
      @course_group = CourseGroup.new(params[:course_group])
      @course_group.courses.each do |course|
      course.school = @current_school
      end
      @current_school.course_groups << @course_group

      if @course_group.save
      redirect_to course_groups_path, :notice =>"Curso creado", :subdomain => @current_school.id
      else
      render 'new'
      end
      end

      1. End controller

      class Course
      include Mongoid::Document

      field :name

      referenced_in :school
      references_and_referenced_in_many :course_groups
      references_and_referenced_in_many :professors
      end

      1. encoding : utf-8
        class School
        include Mongoid::Document
        include Mongoid::DynamicFinders

      field :name
      key :name

      references_many :students
      references_many :professors
      references_many :courses
      references_many :course_groups

      referenced_in :user

      validates :name, :presence => true
      end

      This is a Course_group saved in the database. It has courses, but those courses do not really exist. They aren't saved in the database.

      { "_id" : ObjectId("4d39b518511ea7043d00000b"), "course_ids" : [ ObjectId("4d39b518511ea7043d00000c"), ObjectId("4d39b518511ea7043d00000d"), ObjectId("4d39b518511ea7043d00000e") ], "name" : "WEEE", "school_id" : "pablo-de-olavide" }

      This used to work in beta.20

            Assignee:
            Unassigned Unassigned
            Reporter:
            Nerian Gonzalo Rodríguez-Baltanás Díaz [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: