@@ -275,7 +275,18 @@ private class ActiveRecordModelFinderCall extends ActiveRecordModelInstantiation
275275 exists ( MethodCall call , Expr recv |
276276 call = this .asExpr ( ) .getExpr ( ) and
277277 recv = getUltimateReceiver ( call ) and
278- resolveConstant ( recv ) = cls .getAQualifiedName ( ) and
278+ (
279+ // The receiver refers to an `ActiveRecordModelClass` by name
280+ resolveConstant ( recv ) = cls .getAQualifiedName ( )
281+ or
282+ // The receiver is self, and the call is within a singleton method of
283+ // the `ActiveRecordModelClass`
284+ recv instanceof SelfVariableAccess and
285+ exists ( SingletonMethod callScope |
286+ callScope = call .getCfgScope ( ) and
287+ callScope = cls .getAMethod ( )
288+ )
289+ ) and
279290 call .getMethodName ( ) = finderMethodName ( )
280291 )
281292 }
@@ -293,18 +304,24 @@ private class ActiveRecordModelClassSelfReference extends ActiveRecordModelInsta
293304 m = this .getCfgScope ( ) and
294305 m .getEnclosingModule ( ) = cls and
295306 m = cls .getAMethod ( )
296- )
307+ ) and
308+ // In a singleton method, `self` refers to the class itself rather than an
309+ // instance of that class
310+ not this .getSelfScope ( ) instanceof SingletonMethod
297311 }
298312
299313 final override ActiveRecordModelClass getClass ( ) { result = cls }
300314}
301315
302- // A (locally tracked) active record model object
303- private class ActiveRecordInstance extends DataFlow:: Node {
316+ /**
317+ * An instance of an `ActiveRecord` model object.
318+ */
319+ class ActiveRecordInstance extends DataFlow:: Node {
304320 private ActiveRecordModelInstantiation instantiation ;
305321
306322 ActiveRecordInstance ( ) { this = instantiation or instantiation .flowsTo ( this ) }
307323
324+ /** Gets the `ActiveRecordModelClass` that this is an instance of. */
308325 ActiveRecordModelClass getClass ( ) { result = instantiation .getClass ( ) }
309326}
310327
0 commit comments