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 many_to_many when one of the models has a prefix to the intersection model association #449

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

bguban
Copy link

@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
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

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