我需要一种干净的方法来检查嵌入式 Mongoid 模型实例上是否存在动态属性。
这适用于顶级模型;
account.attributes.member?("coordinates")
但我的坐标存储在嵌入式模型中,位置。以下抛出未定义的方法错误;
account.locations.attributes.member?("coordinates")
有人知道这样做的方法吗?
我需要一种干净的方法来检查嵌入式 Mongoid 模型实例上是否存在动态属性。
这适用于顶级模型;
account.attributes.member?("coordinates")
但我的坐标存储在嵌入式模型中,位置。以下抛出未定义的方法错误;
account.locations.attributes.member?("coordinates")
有人知道这样做的方法吗?
因为似乎位置是一组嵌入式文档。大概有很多关系。
attributes
数据类型没有方法名称array
。这就是您收到未定义错误的原因。您可以通过以下方式解决此问题
account.locations[0].attributes.member?("coordinates")
或使用检查整个数组中的坐标成员
account.locations.map {|x| [x.id,x.attributes.member? 'coordinates'] }
我最终得到了以下结果,因为我什至不能依赖位置属性在那里;
如果@account.locations?如果 !@account.locations[0].coordinates?如果@account.locations[0].coordinates.blank?#做某事结束结束结束
有点乱,但它有效。
感谢您的回复拉梅什!
请注意,由于某种原因,我无法使代码格式正常工作...对不起