请解释原因:
User.first.comments.class => Array
User.first.comments.missing_method => undefined method `missing_method' for []:ActiveRecord::Relation为什么第一行中的类是Array,另一行是Relation?
发布于 2012-09-19 21:29:07
User.first.comments实际上返回一个AssociationProxy对象。当你调用User.first.comments.class时,你会得到一个数组,因为class方法是未定义的,并且在别处被委托了。
发布于 2012-09-19 20:45:38
因为User.first.comments方法存在,并且它有返回值数组,而对于关系注释,missing_method不存在。注释是具有返回值数组的ActiveRecord::Relation方法。
https://stackoverflow.com/questions/12494817
复制相似问题