How to get activerecord associations via reflection

后端 未结 3 764
孤街浪徒
孤街浪徒 2020-12-23 00:04

For normal columns, you can get at them via the columns class method. However, associations may be named something quite different if the foreign_key

3条回答
  •  再見小時候
    2020-12-23 00:27

    For an ActiveRecord object I use:

    object._reflections
    

    So, I can manipulate the Hash returned. For instance:

    object._reflections.keys.each do |key|
        object.public_send(key).destroy_all
    end
    

    The above example delete all the relationships from database.

提交回复
热议问题