简体   繁体   English

如何在Rails中将活动记录与关联实现图?

[英]How can i implement a graph with associations of my active record in rails?

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 . 我在应用程序级别上具有Rails Active record上的所有外键关联,而在db上没有任何关联,我想用150个表的这些反射实现一个图(数据结构),并且我现在可以一次性获得每个模型的所有反射吗?像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">,

检索所有模型的反射的一种可能性是迭代ApplicationRecord类的后代。

all_reflections = ApplicationRecord.descendants.map(&:reflections)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM