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?

-- 
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.

Reply via email to