rom icon indicating copy to clipboard operation
rom copied to clipboard

Nested association assignment on :create command execution

Open estepnv opened this issue 4 years ago • 1 comments

The example described here https://rom-rb.org/5.0/learn/repositories/writing-aggregates/ Creating a record along with associated records raises

     ROM::Struct::MissingAttribute:
       undefined method `[]=' for #<ROM::Struct::ProCompany:0x00007ffed0791040>
       Did you mean?  [] (attribute not loaded?)
     # /Users/username/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rom-core-5.0.2/lib/rom/struct.rb:105:in `rescue in method_missing'
     # /Users/username/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rom-core-5.0.2/lib/rom/struct.rb:102:in `method_missing'
     # /Users/username/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/transproc-1.1.0/lib/transproc/array/combine.rb:26:in `block in add_groups_to_element'

It tries to use #[]= operator on ROM::Struct while its not defined on it

To Reproduce

module MyModule

  class Companies < ROM::Relation[:sql]
    gateway :gateway_1

    auto_struct true

    schema(:companies, infer: true, as: :pro_companies) do
      associations do
        has_many :pro_role_groups, as: :role_groups
      end
    end

  end
end

module MyModule

  class RoleGroups < ROM::Relation[:sql]
    gateway :gateway_1

    auto_struct true

    schema(:role_groups, infer: true, as: :pro_role_groups) do

      associations do
        belongs_to :pro_company, as: :company
      end

    end

  end
end

module MyModule
  class CompanyRepository < ::ROM::Repository[:pro_companies]

    def create_with_default_role_groups(_company)
      company = _company.dup
      company.fetch(:role_groups, []).each do |rg|
        rg[:created_at] = Time.now
        rg[:updated_at] = Time.now
      end

      create_company = pro_companies.combine(:role_groups).command(:create) 
      create_company.call(company) # OOPS! 
    end

  end
end

Expected behavior

It should not raise error

Environment

  • Affects my production application: NO
  • Ruby version: 2.6.3
  • OS: MacOS Mojave

estepnv avatar Jul 22 '19 16:07 estepnv

Please provide an executable script that reproduces the problem

solnic avatar Jul 22 '19 17:07 solnic