ruby on rails - How to get all the associated models from ActiveRecord object? -
for example have
class order < activerecord::base has_many :shippings has_one :contact_information belongs_to :shop end
how array of associated objects order. example
order.associations # [:shipping, :contact_information, :shop]
order.reflect_on_all_associations.map(&:class_name)
you can pass type of relation parameter: order.reflect_on_all_associations(:has_one)
read activerecord::reflection::classmethods
edit
just realised, you've asked object's associated models.
so, having i've showed, can along following lines:
@some_order = order.first associated_models = @some_order.class.reflect_on_all_associations.map(&:class_name)
Comments
Post a Comment