0

I have all foreign key associations on app level with rails Active record and none on db, i want to implement a graph (Data structure) with these reflections of 150 tables and also can i get all reflections of every model in one go currently i am getting reflection for every single table manually like this User.reflections.

And get this:

{"audits"=>
  #<ActiveRecord::Reflection::HasManyReflection:0x00007faa1a75fc08
   @active_record=
    User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, confirmation_token: string, confirmed_at: datetime, confirmation_sent_at: datetime, unconfirmed_email: string, created_at: datetime, updated_at: datetime, first_name: string, last_name: string, date_of_birth: datetime, guid: string, mobile_no: string, gender: integer, status: integer, authentication_token: string, is_internal_employee: boolean),
   @association_scope_cache={},
   @automatic_inverse_of=nil,
   @constructable=true,
   @foreign_type="audits_type",
   @klass=nil,
   @name=:audits,
   @options={:as=>:auditable, :class_name=>"Audited::Audit"},
   @plural_name="audits",
   @scope=#<Proc:0x00007faa1a75fd20@/Users/vipulmaan/.rvm/gems/ruby-2.4.3/gems/activerecord-5.1.4/lib/active_record/associations/builder/association.rb:55>,
   @scope_lock=#<Thread::Mutex:0x00007faa1a75f618>,
   @type="auditable_type">,
Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32

1 Answers1

1

One possibility to retrieve the reflections of all your models would be to iterate over the descendants of your ApplicationRecord class.

all_reflections = ApplicationRecord.descendants.map(&:reflections)
Marcus Ilgner
  • 6,935
  • 2
  • 30
  • 44
  • 1
    Also `ActiveRecord::Base.descendants` after `Rails.application.eager_load!`: https://stackoverflow.com/a/10712838/5239030 – iGian Jan 19 '19 at 10:59