こんにちは。コーテッグのこやまです。
モデルに対してallやwhereメソッドを使って返ってくるのは、
ActiveRecord::Relationのインスタンスですが、
元クラスを取得したいときってありますよね。
RailsのActiveRecord::Relationでは
.classではclassを取得できます。
.klassでmodelを取得できます。
.modelでもmodelを取得できます。
自分でも何言っているかわからないのでコードを見てみましょう。
hoges = Hoge.where(id: [1..10]) // .class(Objectクラスのインスタンスメソッド) hoges.class => Hoge::ActiveRecord_Relation // .klass(Active Record RelationのAttributes) hoges.klass => Hoge(id: integer, fuga_id: integer, .....) Returns the class of the target. belongs_to polymorphic overrides this to look at the polymorphic_type field on the owner. // .model(Active Record RelationのAttributes) hoges.model => Hoge(id: integer, fuga_id: integer, .....)
ということでした。.classはRubyで.klassはRailsって感じですかね。
私は普段は.modelを使っています。