Skip to content

Commit

Permalink
Handle properly aliased associations
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Dec 6, 2023
1 parent c2c244a commit 4787e48
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rom/factory/attributes/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/integration/rom/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/shared/relations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -45,6 +46,7 @@
associations do
belongs_to :user
belongs_to :user, as: :author
belongs_to :task, as: :parent
end
end
end
Expand Down

0 comments on commit 4787e48

Please sign in to comment.