使用 MongoMapper 验证关联
模型:
class User
include MongoMapper::Document
many :properties
validates_associated :properties
...
end
class Property
include MongoMapper::Document
belongs_to :user
many :services
validates_associated :services
...
end
class Service
include MongoMapper::Document
belongs_to :property
...
end
在控制器中:
@property.save #returns false and true as expected
current_user.save #returns always true why?
它似乎没有使用 current_user.save 方法验证属性模型。 为什么? :(
Models:
class User
include MongoMapper::Document
many :properties
validates_associated :properties
...
end
class Property
include MongoMapper::Document
belongs_to :user
many :services
validates_associated :services
...
end
class Service
include MongoMapper::Document
belongs_to :property
...
end
In controller:
@property.save #returns false and true as expected
current_user.save #returns always true why?
It seems, that it doesnt validate the Property model with current_user.save method.
Why? :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 MongoMapper 中,分配非嵌入的多个关联会自动保存您关联的记录。但是,如果其中任何记录无效,它们不会默默地保存到数据库中。下次您请求关联时,MongoMapper 会访问数据库,但什么也没找到。您分配的无效记录消失。
您可以使用
build
方法将对象添加到关联中,而无需保存。我认为关联保存是 MongoMapper 的弱点之一。然而,这并不是一个容易的问题。请参阅 github 上的问题 #233 了解有关挑战的讨论。
In MongoMapper, assigning a non-embedded many association automatically saves the records you're associating. But if any of those records are invalid, they silently aren't saved to the database. The next time you ask for the association MongoMapper goes to the database and finds nothing. The invalid records you assigned disappear.
You can use the
build
method to add objects to the association without saving.I consider association saving to be one of MongoMapper's weak point. However, it's not an easy problem. See issue #233 on github for a discussion of the challenges.