Model reflections

I’d like to suggest to improve duck typing to obtain the model class:

I found these variations so far:

  • ActiveRecord::Base#class
  • ActiveRecord:Relation#model
  • ActiveRecord:Reflection#klass
  • ActiveModel:Name @klass

I’d like to suggest a duck method to obtain the model from any object (where that makes sense)

  • we should not reuse #model (that would collide with legacy code)
  • I’d suggest: #model_class (or #model_klass)

There are several methods from ActiveModel::Naming that may have what you need.

https://github.com/rails/rails/blob/main/activemodel/lib/active_model/naming.rb

The docs have several examples on how to use it. Let me know if this works for you!

Hi Julian, all thanks for your reply.

I got around that problem by being more specific.

But the idea still is on the road. The Model is a central concept within the Rails orm

But guess what? It’s not even a class on it’s own. It is currently implemented as a mixin. This looks to me like a bad decission form the days of yore.

I stumbled around that when I tried to make a recursion. just walking the tree, new classes came up, each having a different api.

We can’t fix this in rails v6 – don’t change the api but maybe v7 is still open for improvements?

It get’s harder though, to not break the legacy codes.

~eike