require 'rom'
require 'rom-factory'
rom = ROM.container(:sql, 'sqlite::memory') do |conf|
conf.default.create_table(:companies) do
primary_key :id
column :name, String, null: false
end
conf.default.create_table(:users) do
primary_key :id
column :name, String, null: false
column :company_id, Integer, null: false
end
conf.default.create_table(:tasks) do
primary_key :id
column :name, String, null: false
column :user_id, Integer, null: false
end
conf.relation(:companies) do
schema(:companies, infer: true) do
associations do
has_many :users
end
end
end
conf.relation(:users) do
schema(:users, infer: true) do
associations do
belongs_to :companies, as: :company
has_one :tasks
end
end
end
conf.relation(:tasks) do
schema(:tasks, infer: true) do
associations do
belongs_to :users, as: :user
end
end
end
end
MyFactory = ROM::Factory.configure do |c|
c.rom = rom
end
MyFactory.define(:company) do |f|
f.name { fake(:company, :name) }
f.association(:users, count: 2)
end
MyFactory.define(:user) do |f|
f.name { fake(:name) }
f.association(:company)
f.association(:tasks)
end
MyFactory.define(:task) do |f|
f.name { fake(:name) }
f.association(:user)
end
Hi all!
It seems that when we have a cascade has_many or has_one with 3 or more factories some records are not created properly.
In the example below, when I call MyFactory[:company] the actual result is:
#<ROM::Struct::Company id=1 name="Reilly-Labadie" users=[#<ROM::Struct::User id=1 name="Bernie Mueller" company_id=1>, #<ROM::Struct::User id=2 name="Jeff Dickens" company_id=1>]>
But should be:
#<ROM::Struct::Company id=1 name="Reilly-Labadie" users=[#<ROM::Struct::User id=1 name="Bernie Mueller" company_id=1 tasks=#<ROM::Struct::Task id=18 name="Jessie Roob" user_id=1>>, #<ROM::Struct::User id=2 name="Jeff Dickens" company_id=1 tasks=#<ROM::Struct::Task id=19 name="Jessie Roob" user_id=2>>]>