An example schema is "book belongs_to library"
r = Book.reflect_on_all_associations(:belongs_to).first
The above would give the first reflection. I can then do "r.name" to get "library" (well, assuming no other belongs_to).
So my question is... If I wanted to dynamically access the "Library" class, how would I do that?
I don't want to manually use "Library.all" (etc), since I'm trying to make it a generic thing.
My initial attempt is something like this:
r.name.singularize.classify.constantize
But that seems awkward.... is there a more proper way?
I just tried this in my console:
k = Post.reflect_on_all_associations(:belongs_to).first.active_record
# => User
k.first
# => returns the first user
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With