-
Notifications
You must be signed in to change notification settings - Fork 464
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
STI breaks arrange scoping #676
Comments
Ancestry does ignore the base class used for the queries. The issue is when you fetch a Fruit and ask for the path of this fruit. Are there particular cases where you expect it to ignore the class used for the query and cases where you expect it to respect the class used for the query? |
I feel like for our app, we would always want to respect the class, since we have a restriction on parents being of the same STI type, so the trees are always made up of unique types. In our app we have a base This issue is appearing when we select the tree to display to the user. We only want to select a tree a) for a particular customer and b) for a particular sub-type and arrange that for display. Does that give some context on what we're trying to achieve? |
thanks @wooly All of the STI examples I've seen have a class and the children are a type in the same class list. So My day job does have a generic hierarchy type class but it breaks down and I am moving away from this, albeit slowly. Why are you using STI at all? Does it just buy you not having to define 10 db tables? # Group.first.fruit.arrange # dropping the :type from the where clause
Fruit.where(:group_id => Group.first.id).arrange
class Group # als
has_many :fruit, -> { where(:type => 'Fruit') }
end And instead, it is returning every hierarchy |
I've found what I think is a bug with the
arrange
method when used with a chained scope in an STI environment (a bit of an obscure one, I realise!).Say we have a
Node
with a sub-class ofFruit
. An additional class ofGroup
is also added to exhibit the error. AGroup
has a has_many association to both nodes and fruits.When you call arrange with the base class of node, the query to fetch the nodes is correct. However, when you call arrange with the sub-class of node, the query to fetch the nodes fetches everything and filters in-memory.
I've made an example rails app exhibiting the behaviour here:
https://github.com/wooly/ancestry_arrange_error/tree/main
If you create the db with
rails db:create
then runrails runner scripts/strange.rb
, you'll see the issue.Any ideas?
The text was updated successfully, but these errors were encountered: