On Saturday, May 2, 2015 at 6:09:36 AM UTC-7, Janko Marohnić wrote:
>
> Hi Jeremy,
>
> There are often situations where you want to do subselects with associated
> tables, like getting a count from a has_many association:
>
> Quiz.select_append(Question.where(quiz_id: :quizzes_id).select{count{}.*}.
> as(:questions_count))
>
> However, I don't like here that you need to repeat the ID joining. If you
> specified `Quiz.has_many :questions`, the Quiz class already knows what is
> the associated class, and how are the foreign and primary keys matched. So
> it doesn't feel DRY.
>
> I read an answer that you gave to one previous question
> <https://groups.google.com/forum/#!topic/sequel-talk/muJ14cXsnpA>, where
> you gave 3 different ways how to get a count for a has_many associations.
> However, I find it a bit difficult to wrap my head around using joins and
> grouping for this task, I would much rather use subselects.
>
> My proposition would be do add something like
> Sequel::Model.association_select, so that you can do
>
> class Quiz < Sequel::Model
> has_many :questions
>
> dataset_module do
> def with_questions_count
> select_append(association_select(:questions).select{count{}.*}.as(:
> questions_count))
> end
> end
> end
>
> So, `association_select(:questions)` here would be an equivalent of
> `Question.where(quiz_id: quizzes_id)`. For the partical case of counts, I
> decided that I'm much better of with counter caches (using
> sequel_postgresql_triggers), but for other subselects I would love this
> feature. I think it really shines when you have more complex associations,
> for example with custom datasets, using pg_array_associatons etc.
>
> What do you think about this idea?
>
I think this would have fairly limited applicability, so if it was included
in Sequel, it would have to be in a plugin. I'm curious what other people
think of the idea, though. Does anyone have an opinion about whether
something like this should be shipped with Sequel?
It may be worthwhile to implement this as an external plugin that people
could try out. It would be fairly easy to merge it into Sequel later if
there is community support for it.
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 http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.