From d97e7229b685b7d5fce309307efa879881b6d1f2 Mon Sep 17 00:00:00 2001 From: Peter Solnica Date: Sat, 2 Dec 2023 14:10:14 +0000 Subject: [PATCH] Handle properly aliased associations --- lib/rom/factory/attributes/association.rb | 2 +- spec/integration/rom/factory_spec.rb | 16 ++++++++++++++++ spec/shared/relations.rb | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/rom/factory/attributes/association.rb b/lib/rom/factory/attributes/association.rb index 164eca8..f661c7e 100644 --- a/lib/rom/factory/attributes/association.rb +++ b/lib/rom/factory/attributes/association.rb @@ -96,7 +96,7 @@ def call(attrs, persist: true) def parent_traits @parent_traits ||= if assoc.target.associations.key?(assoc.source.name) - traits + [assoc.target.associations[assoc.source.name].name => false] + traits + [assoc.target.associations[assoc.source.name].key => false] else traits end diff --git a/spec/integration/rom/factory_spec.rb b/spec/integration/rom/factory_spec.rb index f0e336a..338f5ab 100644 --- a/spec/integration/rom/factory_spec.rb +++ b/spec/integration/rom/factory_spec.rb @@ -184,6 +184,22 @@ expect(task.author.first_name).to eql("John") end end + + context "with a self-ref association" do + before do + factories.define(:task) do |f| + f.title { "A Task" } + f.association(:parent) + end + end + + it "creates the associated record with provided attributes" do + task = factories[:task, title: "Foo", parent: {title: "Bar"}] + + expect(task.title).to eql("Foo") + expect(task.parent.title).to eql("Bar") + end + end end context "one-to-one-through" do diff --git a/spec/shared/relations.rb b/spec/shared/relations.rb index b9e5fc2..59018c8 100644 --- a/spec/shared/relations.rb +++ b/spec/shared/relations.rb @@ -19,6 +19,7 @@ conn.create_table?(:tasks) do primary_key :id foreign_key :user_id, :users + foreign_key :task_id, :tasks, null: true column :title, String, null: false end @@ -45,6 +46,7 @@ associations do belongs_to :user belongs_to :user, as: :author + belongs_to :task, as: :parent end end end