Rails 和 MongoDB 以及 MongoMapper

发布于 2024-09-01 03:26:53 字数 1244 浏览 4 评论 0原文

我是 Rails 开发的新手,我也开始使用 MongoDB。

我一直在关注这个关于 Rails 复杂表单的 Railscast 教程,但我我使用 MongoDB 作为我的数据库。我在插入文档及其子文档并将数据检索到编辑表单时没有任何问题,但是当我尝试更新它时,我收到此错误

未定义方法“assert_valid_keys”为 false:FalseClass

这是我的实体类

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

结束 ?

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

有谁知道这里发生了什么 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝色星空 2024-09-08 03:26:53

您的任务类是什么样的?它使用EmbeddedDocument吗?如果没有,您是否在其中声明了project_id的密钥?

更新 - 这是由于 save(false) 造成的,执行 save(:validate => false) 就可以设置了。

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.

空城之時有危險 2024-09-08 03:26:53

将任务实体从 EmbeddedDocument 更改为 Document,并删除
validates_ Associate:项目中的任务,现在正在更新、添加和
从更新项目中删除任务。

非常感谢 x1a4 和 John Nunemaker 的帮助:-)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文