From f55a5b9f702b980c3235895b04e87c32bc3ed875 Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Tue, 26 Jul 2022 09:49:35 +0300 Subject: [PATCH] fix has one foreign/primary keys --- lib/motor/build_schema/load_from_rails.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/motor/build_schema/load_from_rails.rb b/lib/motor/build_schema/load_from_rails.rb index 8a7c755a..ae9bd1a5 100644 --- a/lib/motor/build_schema/load_from_rails.rb +++ b/lib/motor/build_schema/load_from_rails.rb @@ -238,13 +238,18 @@ def build_action_text_column(name, model, ref) end def build_reference(model, name, reflection) + primary_key = reflection.polymorphic? ? 'id' : reflection.join_primary_key + foreign_key = reflection.join_foreign_key + + primary_key, foreign_key = foreign_key, primary_key if reflection.has_one? + { name: name, display_name: model.human_attribute_name(name), model_name: reflection.polymorphic? ? nil : reflection.klass.name.underscore, reference_type: reflection.belongs_to? ? 'belongs_to' : 'has_one', - foreign_key: reflection.join_foreign_key, - primary_key: reflection.polymorphic? ? 'id' : reflection.join_primary_key, + foreign_key: foreign_key, + primary_key: primary_key, options: reflection.options.slice(:through, :source), polymorphic: reflection.polymorphic?, virtual: false