Вы можете использовать хеш класса reflections
для этого. Там могут быть более простые способы, но это работает:
# say you have a class Thing
class Thing < ActiveRecord::Base
belongs_to :foo
belongs_to :bar
end
# this would return a hash of all `belongs_to` reflections, in this case:
# { :foo => (the Foo Reflection), :bar => (the Bar Reflection) }
reflections = Thing.reflections.select do |association_name, reflection|
reflection.macro == :belongs_to
end
# And you could iterate over it, using the data in the reflection object,
# or just the key.
#
# These should be equivalent:
thing = Thing.first
reflections.keys.map {|association_name| thing.send(association_name) }
reflections.values.map {|reflection| thing.send(reflection.name) }