1
votes

Here is some simplified code:

class Page
  include Mongoid::Document
  has_many :content, class_name: 'Content', dependent: destroy, autosave: false
  accepts_nested_attributes_for :content
  field :title, type: String
end

class Content
  include Mongoid::Document
  belongs_to :page
  field :text, type: String
end

class PagesController < ApplicationController
  def update
    @page = Page.unscoped.find(params['id'])
    @page.assign_attributes params.require(:page)

    puts @page.reflect_on_association(:content)
    # {:relation=>Mongoid::Relations::Referenced::Many, :extend=>nil, :inverse_class_name=>"Page", :name=>:content, :class_name=>"Content", :dependent=>:destroy, :autosave=>false, :validate=>true}
  end
end

I have a controller spec that creates a page and associated content from factories and tries to patch the page. Whatever I send in page paramas (just the title or with content_attributes) it never saves anything, just as expected.

If I test this in dev, when the params consist just of Page.title, nothing is updated; if there is content attributes array in the params, the content gets updated.

The only thing I want is to assign new params to Page and associated objects, and then later to either manually persist or reject. What am I doing wrong?

  • rails 4.1.9
  • mongoid ~> 4.0.0

===

Update:

In my real project Content model, the field :text is localized; after I changed it to a simple String field, the spec fails as well (persists the field even though the autosave is false and save was never called).

1

1 Answers

0
votes

try adding validates_associated :page to Content model