Find the associations for an ActiveRecord class at run-time? Find the associations for an ActiveRecord class at run-time? ruby ruby

Find the associations for an ActiveRecord class at run-time?


I think the ActiveRecord::Reflection class may be what you're looking for. From the documentation:

  Account.reflect_on_all_associations             # returns an array of all associations  Account.reflect_on_all_associations(:has_many)  # returns an array of all has_many associations


Sounds like a pretty silly thing to do run-time. What exactly are you trying to achieve? I assume that there are a simple and more commonly used solution to whatever your problem is.

If I had to, I'd use TheModel.read_inheritable_attribute(:reflections).