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

Fix multiple association block use to prevent ROM::AssociationSet registry error #683

Open
bkuhlmann opened this issue Mar 8, 2023 · 1 comment

Comments

@bkuhlmann
Copy link

Describe the bug

Hello. 👋 I ran into an issue that tripped me up due the error being rather confusing to debug:

rom-core-5.3.0/lib/rom/registry.rb:91:in `block in fetch': :account doesn't exist in ROM::AssociationSet[:integrations] registry (ROM::ElementNotFoundError)

In hindsight, the error makes a bit more sense now but when seeing it for the first time it took a while to realize that multiple association blocks won't work (which was an accident on my part due to being too hasty).

To Reproduce

Here's the inline script I used to reproduce (extracted from my Hanami application):

Script
#! /usr/bin/env ruby
# frozen_string_literal: true

# Save as `snippet`, then `chmod 755 snippet`, and run as `./snippet`.

require "bundler/inline"

gemfile true do
  source "https://rubygems.org"

  gem "amazing_print"
  gem "debug"
  gem "pg"
  gem "rom"
  gem "rom-sql"
  gem "sequel"
end

class Account < ROM::Relation[:sql]
  schema :accounts, infer: true
end

class Provider < ROM::Relation[:sql]
  schema :providers, infer: true
end

class Integration < ROM::Relation[:sql]
  schema :integrations, infer: true do
    associations { belongs_to :account }
    associations { belongs_to :provider }
  end
end

configuration = ROM::Configuration.new :sql, "<redacted>"
[Account, Provider, Integration].each { |relation| configuration.register_relation relation }
container = ROM.container configuration
repository = ROM::Repository.new container

puts repository.integrations.combine(:account).to_a.inspect
Error ``` /Users/bkuhlmann/.cache/frum/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rom-core-5.3.0/lib/rom/registry.rb:91:in `block in fetch': :account doesn't exist in ROM::AssociationSet[:integrations] registry (ROM::ElementNotFoundError) from /Users/bkuhlmann/.cache/frum/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rom-core-5.3.0/lib/rom/registry.rb:88:in `fetch' from /Users/bkuhlmann/.cache/frum/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rom-core-5.3.0/lib/rom/registry.rb:88:in `fetch' from /Users/bkuhlmann/.cache/frum/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rom-core-5.3.0/lib/rom/relation.rb:288:in `node' from /Users/bkuhlmann/.cache/frum/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rom-core-5.3.0/lib/rom/support/memoizable.rb:50:in `block (2 levels) in define_memoizable_names!' from /Users/bkuhlmann/.cache/frum/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rom-core-5.3.0/lib/rom/relation.rb:271:in `block in nodes' from /Users/bkuhlmann/.cache/frum/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rom-core-5.3.0/lib/rom/relation.rb:268:in `each' from /Users/bkuhlmann/.cache/frum/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rom-core-5.3.0/lib/rom/relation.rb:268:in `reduce' from /Users/bkuhlmann/.cache/frum/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rom-core-5.3.0/lib/rom/relation.rb:268:in `nodes' from /Users/bkuhlmann/.cache/frum/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rom-core-5.3.0/lib/rom/relation.rb:252:in `combine' from /Users/bkuhlmann/.cache/frum/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rom-core-5.3.0/lib/rom/support/memoizable.rb:50:in `block (2 levels) in define_memoizable_names!' from /Users/bkuhlmann/Engineering/Misc/snippet:43:in `' ```

Expected behavior

I guess I would have expected to see an error like "multiple association blocks detected, there can be only one" or something along those lines. 😅

The solution is to use:

associations do
  belongs_to :account
  belongs_to :provider
end

Instead of:

associations { belongs_to :account }
associations { belongs_to :provider }

Maybe if multiple association blocks are detected, a better error could be provided? This isn't a huge priority but more of a nice to have I suppose.

My environment

  • Affects my production application: YES (but I've resolved the issue)
  • Ruby version: ruby 3.2.1 (2023-02-08 revision 31819e82c8) +YJIT [arm64-darwin22.3.0]
  • OS: macOS 13.2.1
@solnic solnic added this to the 6.0.0 milestone Mar 8, 2023
@solnic
Copy link
Member

solnic commented Mar 8, 2023

Thanks for reporting this. I think it just works in main but I need to double check, so leaving this open but targeting 6.0.

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