請解釋原因:的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