Skip to content

Fix many_to_many when one of the models has a prefix to the intersection model association #449

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

Conversation

bguban
Copy link
Contributor

@bguban bguban commented Mar 13, 2023

Fix many_to_many when one of the models is under a namespace and has a prefix to the intersection model association.

class Hc::Validator < ApplicationRecord
  has_many :validator_universes
  has_many :universes, through: :validator_universes, inverse_of: :hc_validators
end

class Hc::ValidatorResource < ApplicationResource
  many_to_many :universes, resource: UniverseResource, foreign_key: { hc_validator_universes: :validator_id }
end

class Hc::ValidatorUniverse < ApplicationRecord
  belongs_to :validator
  belongs_to :universe
end

class Universe < ApplicationRecord
  has_many :hc_validator_universes, class_name: 'Hc::ValidatorUniverse'
  has_many :hc_validators, class_name: 'Hc::Validator', through: :hc_validator_universes, source: :validator
end

class UniverseResource < ApplicationResource
  many_to_many :hc_validators, resource: Hc::ValidatorResource, foreign_key: { validator_universes: :universe_id }
end

bguban and others added 2 commits March 13, 2023 17:18
…a prefix to the intersection model.

class Hc::Validator < ApplicationRecord
  has_many :validator_universes
  has_many :universes, through: :validator_universes, inverse_of: :hc_validators
end

class Hc::ValidatorResource < ApplicationResource
  many_to_many :universes, resource: UniverseResource, foreign_key: { hc_validator_universes: :validator_id }
end

class Hc::ValidatorUniverse < ApplicationRecord
  belongs_to :validator
  belongs_to :universe
end

class Universe < ApplicationRecord
  has_many :hc_validator_universes, class_name: 'Hc::ValidatorUniverse'
  has_many :hc_validators, class_name: 'Hc::Validator', through: :hc_validator_universes, source: :validator
end

class UniverseResource < ApplicationResource
  many_to_many :hc_validators, resource: Hc::ValidatorResource, foreign_key: { validator_universes: :universe_id }
end
@jkeen
Copy link
Collaborator

jkeen commented Feb 27, 2024

@bguban Mind adding a test for this? I'm not quite understanding the issue

@bguban
Copy link
Contributor Author

bguban commented Oct 29, 2024

Hi @jkeen. sorry for the late response. the problem is pretty simple. graphiti fails with

Graphiti::Errors::SideloadQueryBuildingError (          Hc::ValidatorResource: error occurred while sideloading "universes"!

          The error was raised while attempting to build the scope for the associated Resource.

          Read more about sideload scoping here: www.graphiti.dev/guides/concepts/resources#customizing-scope

          Here's the original, underlying error:

          NoMethodError: undefined method `klass' for nil
          /Users/bguban/.rvm/gems/ruby-3.3.3@market_data/gems/graphiti-1.3.9/lib/graphiti/adapters/active_record/many_to_many_sideload.rb:4:in `through_table_name'
...

when I make a request like /api/v1/hc/validators?include=universes. It's because ValidatorResource and ValidatorUniverse is within 'Hc' namespace but the universe isn't.

Honestly say it's not clear why it takes parent_resource_class instead of just take resource_class. Hopefully soon I'll have time to get dipper into it

@bguban
Copy link
Contributor Author

bguban commented Mar 1, 2025

Hi @jkeen, I've added tests. Not sure about the models (Stocks and Shop) so if you can find better names/meanings for the models and the namespace let me know.

@bguban
Copy link
Contributor Author

bguban commented Mar 14, 2025

@jkeen have you had a chance to take a look at this PR? I'm not sure if the model names make good sense. I had to create a test when resources from different namespaces communicate via has_many through relation. I'll be glad to hear if you can offer models that would make more sense. Let me know if you have any questions. Thank you.

@jkeen
Copy link
Collaborator

jkeen commented Mar 15, 2025

It looks like there just a few linter issues to resolve before this is good. I don't quite connect the stocks/shops model names as you mentioned, but at the moment I can't think of something better 🤔

@jkeen jkeen merged commit dc28a4f into graphiti-api:master Mar 16, 2025
36 checks passed
github-actions bot pushed a commit that referenced this pull request Mar 16, 2025
## [1.7.8](v1.7.7...v1.7.8) (2025-03-16)

### Bug Fixes

* compare URI-decoded path params ([#482](#482)) ([20b80dd](20b80dd))
* correct issue with many_to_many when one of the models has a prefix to the intersection model association ([#449](#449)) ([dc28a4f](dc28a4f))
* lazy constantize relation resources ([#492](#492)) ([3cc2983](3cc2983))
Copy link

🎉 This PR is included in version 1.7.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants