Rails and MongoDB with MongoMapper

I am new to Rails development and I am getting started with MongoDB too.

I was following this Railscast tutorial on complex forms with Rails, but I am using MongoDB as my database. I have no problem inserting documents with him childs and fetching data into edit form, but when I try to update it I get this error

undefined method `assert_valid_keys' for false: FalseClass

this is my entity class

class Project 
  include MongoMapper::Document

  key :name, String, :required => true
  key :priority, Integer

  many :tasks
  after_update :save_tasks

  def task_attributes=(task_attributes)
    task_attributes.each do |attributes|
    if attributes[:id].blank?
      tasks.build(attributes)
    else
      task = tasks.detect { |t| t.id.to_s == attributes[:id].to_s }  
      task.attributes = attributes
    end
  end    
end

def save_tasks
tasks.each do |t|
  if t.should_destroy?
    t.destroy
  else
    t.save(:validate => false)
  end
end

      

end end

class Task 
include MongoMapper::EmbeddedDocument

key :project_id, ObjectId
key :name, String
key :description, String
key :completed, Boolean

belongs_to :project
attr_accessor :should_destroy

def should_destroy?
  should_destroy.to_i == 1
end 
end

      

Does anyone know what's going on here? Thanks to

+2


a source to share


2 answers


Changed Task object from EmbeddedDocument to Document and removed validates_associated: task from Project, now it updates, adds and removes tasks from project update.



Many thanks to x1a4 and John Nunemaker for their help :-)

0


a source


What does the Task class look like? Does he use EmbeddedDocument? If not, have you declared a key for the project_id in it?



update - because of save(false)

, do save(:validate => false)

, and you should be installed.

+2


a source







All Articles