Skip to content

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

Merged
jkeen merged 9 commits into
graphiti-api:masterfrom
bguban:fix_many_to_many_with_different_association_names
Mar 16, 2025
Merged

Fix many_to_many when one of the models has a prefix to the intersection model association#449
jkeen merged 9 commits into
graphiti-api:masterfrom
bguban:fix_many_to_many_with_different_association_names

Conversation

@bguban

@bguban bguban commented Mar 13, 2023

Copy link
Copy Markdown
Contributor

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

jkeen commented Feb 27, 2024

Copy link
Copy Markdown
Collaborator

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

@bguban

bguban commented Oct 29, 2024

Copy link
Copy Markdown
Contributor Author

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

bguban commented Mar 1, 2025

Copy link
Copy Markdown
Contributor Author

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

bguban commented Mar 14, 2025

Copy link
Copy Markdown
Contributor Author

@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

jkeen commented Mar 15, 2025

Copy link
Copy Markdown
Collaborator

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
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))
@github-actions

Copy link
Copy Markdown

🎉 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