RAILS: How to get has_many associations of a model RAILS: How to get has_many associations of a model ruby ruby

RAILS: How to get has_many associations of a model


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]


For Example you could try :

aux=Array.newPage.reflections.each { |key, value| aux << key if value.instance_of?(ActiveRecord::Reflection::AssociationReflection) }

Hi Pioz , Have a Nice Day!


Found the solutions:

def self.get_macros(macro)  res = Array.new  self.reflections.each do |k,v|    res << k if v.macro == macro.to_sym  end  return resend