RAILS: How to get has_many associations of a model

后端 未结 3 418
猫巷女王i
猫巷女王i 2020-12-10 01:46

how I can get the has_many associations of a model?

For example if I have this class:

class A < ActiveRecord::Base
  has_many B
  has_many C
end
<         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 02:08

    You should be using ActiveRecord reflections.

    Then you can type something like this:

    A.reflect_on_all_associations.map { |assoc| assoc.name}
    

    which will return your array

    [:B, :C]
    

提交回复
热议问题