型号:
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方法来验证属性模型。为什么?
发布于 2011-12-01 09:10:21
在MongoMapper中,分配一个非嵌入的多个关联会自动保存要关联的记录。但是如果这些记录中的任何一个是无效的,它们都不会默默地保存到数据库中。下一次请求关联时,MongoMapper将转到数据库,但什么也找不到。您分配的无效记录将消失。
user = User.new(:properties => [Property.new])
user.properties # => []
user.valid? # => true您可以使用build方法将对象添加到关联中,而无需保存。
user = User.new
user.properties.build
user.properties # => [#<Property _id: BSON::ObjectId('...0e'), user_id: BSON::ObjectId('...0c')>]
user.valid? # => false我认为关联保存是MongoMapper的弱点之一。然而,这并不是一个简单的问题。有关挑战的讨论,请参阅issue #233 on github。
https://stackoverflow.com/questions/8333277
复制相似问题