2

I'm new to Rails development and I'm starting with MongoDB also.

I have been following this Railscast tutorial about complex forms with Rails but I'm using MongoDB as my database. I'm having no problems inserting documents with it's childs and retrieving the data to the 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 knows whats happening here? Thanks

7
  • Ah, in this case, I think you don't want an explicit project_id in the Task class, because it's already handled by being embedded. Commented May 14, 2010 at 4:35
  • Oh, save(false) is what's failing. With mongomapper, you need to pass a hash of values to save. Do save(:validate => false) if you don't want to validate. Commented May 14, 2010 at 7:32
  • Thanks! but it worked partially, it only updates the existing elements and even add new ones, but it's not deleting and I get this error always "stack level too deep" Commented May 14, 2010 at 18:43
  • How are you doing the delete? Could you make a pastie or gist of the full stacktrace? Commented May 14, 2010 at 19:47
  • Sorry at that time I hadn't added the code to be able to delete an existing task for a project, I have added it now and it seems it still doesn't work, here's the gist for the full Trace gist.github.com/402373 and I even uploaded the project to github for if anyone can help me :) github.com/fcastellanos/rails_mongodb very much appreciate your help Commented May 15, 2010 at 19:52

2 Answers 2

2

What does your Task class look like? Does it use EmbeddedDocument? If not, did you declare a key for project_id in it?

update - it's due to the save(false), do save(:validate => false) and you should be set.

Sign up to request clarification or add additional context in comments.

1 Comment

Dude thanks a lot for the quickness of your answers, I already deleted the project_id in the Task class but it throws the same error, any other ideas?
0

Changed the Task entity from EmbeddedDocument to Document, and deleted the validates_associated: task from Project, it's now working updating, adding and deleting tasks from updating a Project.

Thanks a lot to x1a4 and John Nunemaker for the help :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.