Suppose I have an object Person
, which has_many
:foos
and :bars
.
Given an instance, p (p = Person.new
), how do I programmatically determine what relationships are available?
i.e. p.some_method => ["foo", "bar"]
You can use Active Record Reflections (API here)
In your example:
p.class.reflect_on_all_associations(:has_many).collect {|a| a.name}
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