请解释原因:
User.first.comments.class => Array
User.first.comments.missing_method => undefined method `missing_method' for []:ActiveRecord::Relation
为什么第一个线类是Array而另一个是Relation?
答案 0 :(得分:1)
User.first.comments
实际上返回一个AssociationProxy对象。当您致电User.first.comments.class
时,您将获得一个数组,因为class
方法未定义并被委派给其他人。
答案 1 :(得分:0)
因为方法User.first.comments存在并且它具有返回值数组,并且Relation注释中不存在missing_method。 comments是ActiveRecord ::具有返回值数组的Relation方法。