-
Notifications
You must be signed in to change notification settings - Fork 463
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
Error in MaterializedPath2 descendants SQL? #638
Comments
Seems like it's been that way for a little while, so maybe it's not an error, but I'm trying to build a test, and the I just barely installed the gem, ran the migration, and added Migration: class AddAncestryToDepartments < ActiveRecord::Migration[7.0]
def change
change_table(:departments) do |t|
t.string "ancestry", collation: 'C', null: false, default: ""
t.index "ancestry"
end
end
end Note that I added the Active record class: class Department < ApplicationRecord
has_ancestry
end Test: class DepartmentTest < UnitTest
attr_accessor :department
setup do
self.department = departments(:marketing_department)
end
def test_ancestry
dev = departments(:dev_department)
dev.update!(parent: department)
assert_includes department.descendants, dev # This fails. department.descendants returns []
assert_equal department, dev.parent # this passes
end
end Hack: class Department < ApplicationRecord
has_ancestry
def self.descendants_by_ancestry(ancestry)
arel_table[ancestry_column].matches("%#{ancestry}%", nil, true)
end
end If I do this ☝️ the test passes. |
And with a test too! Really appreciate the notes. Looks like 4-3 broke descendants for materialized_path and materialized_path2 I think #636 overlaps with this and have a good chunk of time this weekend to track it down. The If you are using materialized_path2, then set the default to If you are using Thanks again for the test and let me know if the db default fixes things for now. |
# /usr/bin/env ruby
# test/concerns/repro_638_test.rb
require_relative '../environment'
class Repro638Test < ActiveSupport::TestCase
def test_has_ancestry_in_after_save
AncestryTestDatabase.with_model do |model|
node1 = model.create!
node11 = model.create!
node11.update!(parent: node1)
assert_includes node1.descendants, node11
end
end
end I ran on branch Do you have ideas on now to make my test more similar to yours? DB=sqlite3 FORMAT=materialized_path UPDATE_STRATEGY=ruby ruby test/concerns/repro_638_test.rb
DB=sqlite3 FORMAT=materialized_path2 UPDATE_STRATEGY=ruby ruby test/concerns/repro_638_test.rb |
Seems like |
Also, you may want to check out the migrating a model section. |
closing for now. let me know if you are still having issues. |
The
descendants
scope doesn't seem to be working for MaterializedPath2, and it looks like it's because the SQL is missing a%
at the beginning? I'm definitely not an SQL expert, so I may be missing something here.The
descendants_by_ancestry
method currently hasbut I believe it should be
The main difference is
"#{ancestry}%"
vs"%#{ancestry}%"
.The text was updated successfully, but these errors were encountered: