I have a model User that has defined in user.rb many relationships like
has_many :posts, :dependent => :destroy
has_many :comments, :dependent => :destroy
... and others
How can I programmatically find all such relationships? That is I want to be able to do find all the child models like Post, Comment etc via Rails and not have to look at the user.rb file manually.
How can I do this?
What you want to do is called “reflecting” — whereby your software finds out more about itself, on the fly, at runtime.
In Rails, ActiveRecord supports this. See the Reflection methods.
Added per the docs, you’d call
You’d get back an array of objects that would give you info about all of the has_many associations of your User class.