Rails 和 MongoDB 以及 MongoMapper
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的任务类是什么样的?它使用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)
, dosave(:validate => false)
and you should be set.将任务实体从 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 :-)