Ruby on Rails
OverrideActiveRecordGeneratedMethods (Version #2)

The following may be completely wrong. I was dealing with symptoms that made me think it was tru, but pawing though the ActiveRecord source code, it looks like it might be already be creating the modules I’m suggesting should be created. If so – mea culpa, and please delete this page.

ActiveRecord writes new methods into model classes that extend from ActiveRecord::Base. Among the methods generated are those that return collection objects for associations, and these objects also include generated methods.

If you want to wrap any of these generated methods, you can’t just call the underlying implementation using “super” like you would with a method defined in a base class or an included module. In most cases, some special means are provided for extending these calls using callbacks or internal protected collections of attribute values, but in some cases, one must resort to aliasing the existing method, and having the new implementation call the old one via the alias.

This situation could be greatly improved if ActiveRecord would generate the methods in modules and include the modules into the classes, rather than writing the methods directly into the classes. This way, without any change to the current inheritance patterns, we could now simply rely on “super” wherever where we want to override and extend any method provided by ActiveRecord including the dynamics. We could stop needing to know or care so much about what’s under the hood while customizing our models.

The following may be completely wrong. I was dealing with symptoms that made me think it was tru, but pawing though the ActiveRecord source code, it looks like it might be already be creating the modules I’m suggesting should be created. If so – mea culpa, and please delete this page.

ActiveRecord writes new methods into model classes that extend from ActiveRecord::Base. Among the methods generated are those that return collection objects for associations, and these objects also include generated methods.

If you want to wrap any of these generated methods, you can’t just call the underlying implementation using “super” like you would with a method defined in a base class or an included module. In most cases, some special means are provided for extending these calls using callbacks or internal protected collections of attribute values, but in some cases, one must resort to aliasing the existing method, and having the new implementation call the old one via the alias.

This situation could be greatly improved if ActiveRecord would generate the methods in modules and include the modules into the classes, rather than writing the methods directly into the classes. This way, without any change to the current inheritance patterns, we could now simply rely on “super” wherever where we want to override and extend any method provided by ActiveRecord including the dynamics. We could stop needing to know or care so much about what’s under the hood while customizing our models.