请解释原因:的ActiveRecord ::关系之谜
User.first.comments.class => Array
User.first.comments.missing_method => undefined method `missing_method' for []:ActiveRecord::Relation
为什么在第一线类是数组,另一个是关系?
请解释原因:的ActiveRecord ::关系之谜
User.first.comments.class => Array
User.first.comments.missing_method => undefined method `missing_method' for []:ActiveRecord::Relation
为什么在第一线类是数组,另一个是关系?
由于User.first.comments方法存在,并且它具有返回值数组,并且missing_method对于Relation注释不存在。注释是ActiveRecord ::与返回值数组的关系方法。
User.first.comments
实际上返回一个AssociationProxy对象。由于class
方法未定义并在其他地方委托,因此在调用User.first.comments.class
时您将获得Array。
看起来像你的对 http://apidock.com/rails/ActiveRecord/Associations/AssociationProxy – mingle
你说的那个方法评论返回数组所以为什么当我调用一些方法上的阵列它去的关系? – mingle
因为注释是关系类,所以如果单独调用它“User.first.comments”,它会运行返回数组的方法,但如果在注释中调用数组的方法,则注释是Array类。 – quatermain