私はArelソースとRails3.0のアクティブレコードソースのいくつかを調べましたが、クエリを作成するときにArelがincludes()を使用する能力を変更するかどうかについて自分自身に良い答えを集めることができないようです、 良い方向へ。
返されるアソシエーションレコードについて、2.3.5以前のactiverecord:includeクエリの条件を変更したい場合があります。しかし、私が知る限り、これはすべての:includeクエリに対してプログラムで維持できるわけではありません。
(一部のAR-find-includes make t#{n} .c#{m}は、すべての属性の名前を変更します。これらのクエリに条件を追加して、結合されたセットの結果を制限することもできますが、他のAR-find-includesはn_joins+1番号を実行します。 IDセットに対するクエリの数が繰り返され、ARをハックしてこれらの繰り返されるクエリを編集する方法がわかりません。)
Arelでは、includes()を使用するときに、結果として関連付けられるモデルオブジェクトを指定するActiveRecordクエリを作成できますか?
元:
User :has_many posts( has_many :comments)
User.all(:include => :posts) #say I wanted the post objects to have their
#comment counts loaded without adding a comment_count column to `posts`.
#At the post level, one could do so by:
posts_with_counts = Post.all(:select => 'posts.*, count(comments.id) as comment_count',
:joins => 'left outer join comments on comments.post_id = posts.id',
:group_by => 'posts.id') #i believe
#But it seems impossible to do so while linking these post objects to each
#user as well, without running User.all() and then zippering the objects into
#some other collection (ugly)
#OR running posts.group_by(&:user) (even uglier, with the n user queries)