Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

has_many or has_one with 3 or more factories not workig #60

Open
rinaldifonseca opened this issue Jul 11, 2020 · 0 comments
Open

has_many or has_one with 3 or more factories not workig #60

rinaldifonseca opened this issue Jul 11, 2020 · 0 comments

Comments

@rinaldifonseca
Copy link

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>>]>

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants