I got stuck on this and for sure it's easy, but I just cannot find the solution in the docs.
I have some tree structure and the child where clause that I have to filter with an "exists" sub query:
current_node.children.as("children_nodes").where(Node.where(...).exists)
The Node.where.clause already joins to the children_nodes and it works if I use two different models. But how do I use the alias? Above code will result in:
NoMethodError (undefined method `where' for #<Arel::Nodes::TableAlias
It's so basic, but something I'm missing (I'm too new to arel).
You might be able to use the attribute table_alias
which you can call on an Arel::Table.
Example:
# works
users = User.arel_table
some_other_table = Post.arel_table
users.table_alias = 'people'
users.join(some_other_table)
# doesn't work
users = User.arel_table.alias('people')
some_other_table = Post.arel_table
users.join(some_other_table)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With