Thank you for this. The `:dataset_associations_join` option works nicely
and fits my needs perfectly. The suggestion for the .or clause also works
well and makes sense.

I am having to resist wanting to have my team port all of our AR-based code
to Sequel because I’m loving what we have here (I’m actually working on a
couple of gems that would allow us to do that by removing AR-only
dependencies). What you’ve got here is amazing.

-a

On Tue, Apr 26, 2016 at 7:08 PM, Jeremy Evans <[email protected]>
wrote:

> On Tuesday, April 26, 2016 at 11:48:15 PM UTC+2, Austin Ziegler wrote:
>>
>> I’m pretty sure I’m doing something wrong, but I’m not sure *what* I’m
>> doing wrong with a particular many_to_many setup. I have made a minimal
>> reproduction in a gist, but it’s still fairly complex. I have tried to make
>> sure that there are enough comments in-line.
>>
>> https://gist.github.com/8e424c08ae88a96692b13fee03f5b613
>>
>
> Thanks for this example, it made it much easier to test.
>
>
>>
>> In short, I have Category <=> Product through CategoryProduct.
>> CategoryProduct is almost a pure join table, but also has an attribute,
>> sort_order. When I select products through a particular category, I want
>> the products ordered by (categories_products.sort_order,
>> products.sort_order). This works.
>>
>> Category is also a tree structure using the rcte_tree plug-in, and I want
>> to be able to answer 'what are the products associated with category X and
>> its descendants'? This is something I am trying to do with the
>> `descended_from_category` dataset method on Product.
>>
>> The final twist to what I’m seeing is that I am using the
>> dataset_associations plug-in (I’m using many more than this, but this is
>> the one that seems to be breaking things most).
>>
>> So I’m left with a few questions:
>>
>> 1. Is this a bug?
>>
>
> This isn't a bug.  Dataset associations does not do a join, and therefore
> it is impossible for the query to work, since the products_categories table
> is not joined to the table.
>
> I think the best way to support what you want is to do an optional
> redundant join: http://pastie.org/10814366
>
> You'll need to add the :dataset_associations_join => true association
> option to the association for that to work.
>
> Please give that a shot and let me know if it works for you.  If so, I can
> add tests for it and try to commit it before the next release.
>
> 2. Even if this is a bug, is there a better way to do what I’m trying to
>> do?
>>
>
> Maybe.  This is how I changed it.  I think it does the same thing with
> better performance, but there may be edge cases it doesn't handle:
> http://pastie.org/10814376
>
>
>> 3. Having to do
>> category.this.select(:category_id).union(category.descendants_dataset.select(:category_id)
>> feels clunky compared to Closure Tree for Rails, which has a
>> self_and_descendants method. It might be doing the same thing internally
>> (haven’t looked); is this a worthwhile feature request/PR that I can make?
>>
>
> I don't think a self_and_descendants method makes sense.  If you already
> have the self, why would you want to load it again?  As my example above
> shows, if you want an array, just add the self to the array, and if you
> want a dataset, add an OR condition with self's primary key.  We don't have
> a self_and_children or self_and_ancestors.  The tree plugin does have a
> self_and_siblings method, but I'm not sure that's a good idea (I didn't
> write that plugin).  Even in that case, self_and_siblings is the simpler
> call, and siblings uses it and removes self.
>
> That being said, self_and_descendants shouldn't be difficult to add, and
> without using a union.  Just change the base case in the CTE to use id
> instead of parent_id.
>
> Thanks,
> Jeremy
>
> --
> You received this message because you are subscribed to the Google Groups
> "sequel-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sequel-talk.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Austin Ziegler • [email protected][email protected]
http://www.halostatue.ca/http://twitter.com/halostatue

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to