activegraph icon indicating copy to clipboard operation
activegraph copied to clipboard

What :before and :after association options do?

Open sandric opened this issue 5 years ago • 5 comments

I having troubles understanding and finding docs on before and after options in has_many associacions. I know only that they defined here: https://github.com/neo4jrb/neo4j/blob/95db4f8c8e25004c09fa635f3cd553db81082a96/lib/neo4j/active_node/has_n/association.rb#L208

I'm new to neo4j and started working with code consisting such lines:

has_many :in, :companies, rel_class: :CompanyCategorization, unique: true, after: :recalculate
  has_many :in, :reviews,  type: :CATEGORIZATION, after: :update_cached_counts
  has_many :in, :skips,  type: :SKIPS, model_class: :User, after: :update_cached_counts

recalculate and update_cached_counts are methods, but they seem not to fire, or I dont understand when they should, so can someone clarify when should they invoke so I can debug further, or point me to documentation?

Additional information which could be helpful if relevant to your issue:

Runtime information:

Neo4j database version: 3.0.4 neo4j gem version: 7.2.3 neo4j-core gem version: 6.1.6

sandric avatar Aug 08 '18 13:08 sandric

Aside from a check that the keys are valid, I'm having a bit of trouble finding code and tests that are responsible for those callbacks. @cheerfulstoic were those options removed in favor of ActiveRel?

subvertallchris avatar Aug 08 '18 14:08 subvertallchris

Yeah, I believe that they were

cheerfulstoic avatar Aug 09 '18 13:08 cheerfulstoic

I seem to remember that discussion and decision, but I guess we (I? Probably!) missed the check for valid association options.

So, @sandric, the recommended path is to use ActiveRel in place of those callbacks. Sorry for the confusion.

subvertallchris avatar Aug 09 '18 18:08 subvertallchris

@subvertallchris ok, no problem, I just wanted to understand what it was used before to understand current logic. As I showed in example, lets say there's line has_many :in, :companies, rel_class: :CompanyCategorization, unique: true, after: :recalculate So recalculate method should be called after new node is added to association, or after association query? My current neo4jrb gem version is 7.2.3. @cheerfulstoic can you recall original logic of this option?

sandric avatar Aug 10 '18 02:08 sandric

The changelog shows they were removed with 8.1.0, so I found their specs from 8.0.18 here. You can also see where they're executed here.

It looks like they will fire immediately before and after the association is created. Each callback method needs to accept one argument, which is the node instance on the other side of the association. You can roll back the entire association creation if your before callback returns an explicit false.

Hope this helps!

subvertallchris avatar Aug 10 '18 13:08 subvertallchris