@@ -70,26 +70,30 @@ def has_ancestry options = {}
70
70
71
71
# Create ancestry column accessor and set to option or default
72
72
if options [ :cache_depth ]
73
- # Create accessor for column name and set to option or default
74
- self . cattr_accessor :depth_cache_column
75
- self . depth_cache_column =
76
- if options [ :cache_depth ] == true
77
- options [ :depth_cache_column ] &.to_s || 'ancestry_depth'
78
- else
79
- options [ :cache_depth ] . to_s
73
+ if options [ :cache_depth ] == :virtual
74
+ # NOTE: not setting self.depth_cache_column so the code does not try to update the column
75
+ depth_cache_sql = options [ :depth_cache_column ] &.to_s || 'ancestry_depth'
76
+ else
77
+ # Create accessor for column name and set to option or default
78
+ self . cattr_accessor :depth_cache_column
79
+ self . depth_cache_column =
80
+ if options [ :cache_depth ] == true
81
+ options [ :depth_cache_column ] &.to_s || 'ancestry_depth'
82
+ else
83
+ options [ :cache_depth ] . to_s
84
+ end
85
+ if options [ :depth_cache_column ]
86
+ ActiveSupport ::Deprecation . warn ( "has_ancestry :depth_cache_column is deprecated. Use :cache_depth instead." )
80
87
end
81
- if options [ :depth_cache_column ]
82
- ActiveSupport ::Deprecation . warn ( "has_ancestry :depth_cache_column is deprecated. Use :cache_depth instead." )
83
- end
84
-
85
- # Cache depth in depth cache column before save
86
- before_validation :cache_depth
87
- before_save :cache_depth
88
88
89
- # Validate depth column
90
- validates_numericality_of depth_cache_column , :greater_than_or_equal_to => 0 , :only_integer => true , :allow_nil => false
89
+ # Cache depth in depth cache column before save
90
+ before_validation :cache_depth
91
+ before_save :cache_depth
91
92
92
- depth_cache_sql = depth_cache_column
93
+ # Validate depth column
94
+ validates_numericality_of depth_cache_column , :greater_than_or_equal_to => 0 , :only_integer => true , :allow_nil => false
95
+ depth_cache_sql = depth_cache_column
96
+ end
93
97
else
94
98
# this is not efficient, but it works
95
99
depth_cache_sql = ancestry_depth_sql
0 commit comments