开发者

Rails and MongoDB with MongoMapper

开发者 https://www.devze.com 2022-12-30 11:18 出处:网络
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 p

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


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.


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 :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消