neo4jrb / activegraph

An active model wrapper for the Neo4j Graph Database for Ruby.
http://neo4jrb.io
MIT License
1.4k stars 277 forks source link

Inconsistent results for has_many relation `each_rel/rels` #1696

Open petergebala opened 1 year ago

petergebala commented 1 year ago

Looks like calling each_rel or rels affects the relation and removes objects from it.

Previously we were using: Neo4j database version: 4.4 ActiveGraph version: 11.1.0.alpha.4 Neo4j-driver-ruby: 4.4.3

Code below was working as expected.

Now we try to upgrade gem versions to: Neo4j database version: 4.4 ActiveGraph version: 11.3.1 Neo4j-driver-ruby: 4.4.4

Code example (inline, gist, or repo)

class Skill
  # ...
  has_many :out, :categories, model_class: :SkillCategory, rel_class: :SkillInCategory 
  # ...
end

class SkillCategory < Category
  has_many :in, :contents, model_class: :Skill, rel_class: :SkillInCategory
end

# Later we have test to check if we create uniq relations:
skill.categories << skill_category
skill.categories << skill_category
skill.categories << skill_category

skill.categories.each_rel.count == 1
skill.categories == [skill_category] # Fails, skill.categories relation is empty.
# Problem is with the order of the operations. Looks like calling `each_rel` replaces skill_categories with empty array.
# Real output from console:
[13] pry(#<RSpec::ExampleGroups::Skill::UniqueCategories>)> skill.categories
=> #<AssociationProxy Skill#categories [#<SkillCategory _global_id: "test-skill-category_1000001", _source_id: "1000001", _source_type: "test-skill-category", description: nil, name: "Organized systemic analyzer">]>
[14] pry(#<RSpec::ExampleGroups::Skill::UniqueCategories>)> skill.categories.each_rel.size
=> 1
[15] pry(#<RSpec::ExampleGroups::Skill::UniqueCategories>)> skill.categories
=> #<AssociationProxy Skill#categories []>
[16] pry(#<RSpec::ExampleGroups::Skill::UniqueCategories>)> skill.reload
=> #<Skill _global_id: "test-skill_1000001", _source_id: "1000001", _source_type: "test-skill", name: "Integrated Contextually-Based Ability">
[17] pry(#<RSpec::ExampleGroups::Skill::UniqueCategories>)> skill.categories
=> #<AssociationProxy Skill#categories [#<SkillCategory _global_id: "test-skill-category_1000001", _source_id: "1000001", _source_type: "test-skill-category", description: nil, name: "Organized systemic analyzer">]>
[18] pry(#<RSpec::ExampleGroups::Skill::UniqueCategories>)> skill.categories.rels.size
=> 1
[19] pry(#<RSpec::ExampleGroups::Skill::UniqueCategories>)> skill.categories
=> #<AssociationProxy Skill#categories []>
klobuczek commented 1 year ago

@petergebala One immediate issue I see is the usage of

... rel_class: :SkillInCategory 

If you need a rel_class, because it has some properties you cannot do:

skill.categories << skill_category

You need to create the relationship object with its start, end node, and properties. Maybe you don't need the rel_class.

Your ticket is still probably valid though.

petergebala commented 1 year ago

Yes. Relation doesn't have any property. Thanks for tip. Issue is still there