我需要一种干净的方法来检查嵌入式杂种模型实例上的动态属性。
这适用于顶级模型;
account.attributes.member?("coordinates")
,但我的坐标存储在嵌入式模型,位置。以下将引发未定义的方法错误;
account.locations.attributes.member?("coordinates")
有人知道一种方法吗?
,因为看来这些位置是嵌入式文档的数组。可能有很多关系。
数据类型array
没有方法名称attributes
。这就是为什么您会遇到未定义的错误。您可以通过
account.locations[0].attributes.member?("coordinates")
或使用
在整个数组中检查坐标成员 account.locations.map {|x| [x.id,x.attributes.member? 'coordinates'] }
我最终得到了以下内容
如果 @account.locations? 如果!@account.Locations [0] .Coordinates? 如果 @counduct.locations [0] .coordinates.blank? #做一点事 结尾 结尾结束
有点混乱,但有效。
感谢您的回复Ramesh!
注意,由于某些原因,我无法获得代码格式工作...对不起