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