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

Nested association assignment on :create command execution #556

Open
estepnv opened this issue Jul 22, 2019 · 1 comment
Open

Nested association assignment on :create command execution #556

estepnv opened this issue Jul 22, 2019 · 1 comment

Comments

@estepnv
Copy link

estepnv commented Jul 22, 2019

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 estepnv added the bug label Jul 22, 2019
@estepnv estepnv changed the title ROM::Struct::MissingAttribute undefined method `[]=' for #<ROM::Struct::ProCompany Nested association assignment on :create command execution - ROM::Struct::MissingAttribute undefined method `[]=' for #<ROM::Struct> Jul 22, 2019
@estepnv estepnv changed the title Nested association assignment on :create command execution - ROM::Struct::MissingAttribute undefined method `[]=' for #<ROM::Struct> Nested association assignment on :create command execution Jul 22, 2019
@solnic
Copy link
Member

solnic commented Jul 22, 2019

Please provide an executable script that reproduces the problem

@solnic solnic added this to the 6.0.0 milestone Jun 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Backlog
Development

No branches or pull requests

2 participants